Skip to main content

chipa_webhooks/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum WebhookError {
5    TemplateNotFound(String),
6    TemplateRender(handlebars::RenderError),
7    Http {
8        destination: String,
9        source: reqwest::Error,
10    },
11    Serialize(serde_json::Error),
12    ChannelClosed,
13}
14
15impl fmt::Display for WebhookError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            Self::TemplateNotFound(name) => write!(f, "template not found: {name}"),
19            Self::TemplateRender(e) => write!(f, "template render error: {e}"),
20            Self::Http {
21                destination,
22                source,
23            } => {
24                write!(f, "http error sending to {destination}: {source}")
25            }
26            Self::Serialize(e) => write!(f, "serialization error: {e}"),
27            Self::ChannelClosed => write!(f, "dispatcher channel closed"),
28        }
29    }
30}
31
32impl std::error::Error for WebhookError {}
33
34impl From<handlebars::RenderError> for WebhookError {
35    fn from(e: handlebars::RenderError) -> Self {
36        Self::TemplateRender(e)
37    }
38}
39
40impl From<serde_json::Error> for WebhookError {
41    fn from(e: serde_json::Error) -> Self {
42        Self::Serialize(e)
43    }
44}