summaryrefslogtreecommitdiff
path: root/webhook/src/handler/error.rs
blob: d4fee0704527d6d9fe0fce46c2859f934b92adbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use hyper::{header::ToStrError, Body, Response, StatusCode};

pub struct WebhookError {
    pub code: StatusCode,
    pub message: String,
}

impl WebhookError {
    pub fn new(code: StatusCode, message: &str) -> WebhookError {
        WebhookError {
            code,
            message: message.to_string(),
        }
    }
}

impl Into<Response<Body>> for WebhookError {
    fn into(self) -> Response<Body> {
        Response::builder()
            .status(self.code)
            .body(self.message.into())
            .unwrap()
    }
}

impl From<hyper::Error> for WebhookError {
    fn from(_: hyper::Error) -> Self {
        WebhookError::new(StatusCode::BAD_REQUEST, "invalid request")
    }
}

impl From<ToStrError> for WebhookError {
    fn from(_: ToStrError) -> Self {
        WebhookError::new(StatusCode::BAD_REQUEST, "invalid request")
    }
}