rocket_webhook/
error.rs

1use std::{error::Error, fmt::Display};
2
3/// Possible errors when receiving a webhook
4#[derive(Debug)]
5pub enum WebhookError {
6    /// Signature verification failed
7    Signature(String),
8    /// Missing required header
9    MissingHeader(String),
10    /// Invalid required header
11    InvalidHeader(String),
12    /// Timestamp was invalid and/or not within expected bounds
13    Timestamp(String),
14    /// Error deserializing webhook payload
15    Deserialize(rocket::serde::json::serde_json::Error),
16    /// Error while reading the body of the webhook
17    Read(std::io::Error),
18    /// The webhook was not setup properly on the Rocket instance
19    NotAttached,
20}
21
22impl Display for WebhookError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            WebhookError::Signature(e) => write!(f, "Failed to validate signature: {e}"),
26            WebhookError::MissingHeader(name) => write!(f, "Missing header '{name}'"),
27            WebhookError::InvalidHeader(err) => write!(f, "Header has invalid format: {err}"),
28            WebhookError::Timestamp(time) => write!(f, "Invalid timestamp: {time}"),
29            WebhookError::Deserialize(err) => {
30                write!(f, "Failed to deserialize webhook payload: {err}")
31            }
32            WebhookError::Read(err) => write!(f, "Failed to read webhook body: {err}"),
33            WebhookError::NotAttached => {
34                write!(f, "Webhook of this type is not attached to Rocket")
35            }
36        }
37    }
38}
39
40impl Error for WebhookError {
41    fn source(&self) -> Option<&(dyn Error + 'static)> {
42        match self {
43            WebhookError::Deserialize(err) => Some(err),
44            WebhookError::Read(err) => Some(err),
45            _ => None,
46        }
47    }
48}