Skip to main content

sockudo_http/
errors.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum SockudoError {
5    #[error("Request error: {0}")]
6    Request(#[from] RequestError),
7
8    #[error("Webhook error: {0}")]
9    Webhook(#[from] WebhookError),
10
11    #[error("Configuration error: {message}")]
12    Config { message: String },
13
14    #[error("Validation error: {message}")]
15    Validation { message: String },
16
17    #[error("Encryption error: {message}")]
18    Encryption { message: String },
19
20    #[error("JSON error: {0}")]
21    Json(#[from] sonic_rs::Error),
22
23    #[error("HTTP error: {0}")]
24    Http(#[from] reqwest::Error),
25}
26
27#[derive(Error, Debug)]
28#[error("HTTP request failed")]
29pub struct RequestError {
30    pub message: String,
31    pub url: String,
32    pub status: Option<u16>,
33    pub body: Option<String>,
34}
35
36impl RequestError {
37    pub fn new(
38        message: impl Into<String>,
39        url: impl Into<String>,
40        status: Option<u16>,
41        body: Option<String>,
42    ) -> Self {
43        Self {
44            message: message.into(),
45            url: url.into(),
46            status,
47            body,
48        }
49    }
50}
51
52#[derive(Error, Debug)]
53#[error("Webhook validation failed")]
54pub struct WebhookError {
55    pub message: String,
56    pub content_type: Option<String>,
57    pub body: String,
58    pub signature: Option<String>,
59}
60
61impl WebhookError {
62    pub fn new(
63        message: impl Into<String>,
64        content_type: Option<String>,
65        body: impl Into<String>,
66        signature: Option<String>,
67    ) -> Self {
68        Self {
69            message: message.into(),
70            content_type,
71            body: body.into(),
72            signature,
73        }
74    }
75}