use std::{error::Error, fmt::Display};
#[derive(Debug)]
pub enum WebhookError {
Signature(String),
MissingHeader(String),
InvalidHeader(String),
Timestamp(String),
Deserialize(rocket::serde::json::serde_json::Error),
Read(std::io::Error),
NotAttached,
}
impl Display for WebhookError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WebhookError::Signature(e) => write!(f, "Failed to validate signature: {e}"),
WebhookError::MissingHeader(name) => write!(f, "Missing header '{name}'"),
WebhookError::InvalidHeader(err) => write!(f, "Header has invalid format: {err}"),
WebhookError::Timestamp(time) => write!(f, "Invalid timestamp: {time}"),
WebhookError::Deserialize(err) => {
write!(f, "Failed to deserialize webhook payload: {err}")
}
WebhookError::Read(err) => write!(f, "Failed to read webhook body: {err}"),
WebhookError::NotAttached => {
write!(f, "Webhook of this type is not attached to Rocket")
}
}
}
}
impl Error for WebhookError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
WebhookError::Deserialize(err) => Some(err),
WebhookError::Read(err) => Some(err),
_ => None,
}
}
}