1use std::{error::Error, fmt::Display};
2
3#[derive(Debug)]
5pub enum WebhookError {
6 Signature(String),
8 MissingHeader(String),
10 InvalidHeader(String),
12 Timestamp(String),
14 Deserialize(rocket::serde::json::serde_json::Error),
16 Read(std::io::Error),
18 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}