worker_route/
error.rs

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::http::{
    ContentType, ResponseError, {HttpRequest, HttpResponse, ResponseBuilder},
};
use core::fmt::{Debug, Display};
use http::{
    header::{ToStrError, ACCEPT, CONTENT_TYPE},
    StatusCode,
};
use mime::STAR_STAR;
use serde_json::{json, Value};
use worker::Headers;

/// Top level Worker-Route Error.
#[derive(Debug)]
pub struct Error {
    message: String,
    status_code: StatusCode,
    cause: ErrorCause,
}

/// All possible Error variants that may occur when working with [`worker_route`](crate).
#[derive(Debug)]
pub enum ErrorCause {
    /// Errors occured from [`worker::Error`](https://docs.rs/worker/latest/worker/enum.Error.html)
    Worker(worker::Error),
    /// Errors occured from [`Query`](crate::Query)
    Query,
    /// Errors occured from [`HttpHeaders`](crate::http::headers::HttpHeaders) operations
    Header,
    /// Errors occured from [`ResponseBuilder`](crate::http::ResponseBuilder)
    Json,
}

impl Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl Error {
    /// Creates a new [`Error`].
    pub(super) fn new(message: String, status_code: StatusCode, cause: ErrorCause) -> Self {
        Self {
            message,
            status_code,
            cause,
        }
    }

    /// Returns the underlying error's occurrence
    pub fn cause(&self) -> &ErrorCause {
        &self.cause
    }

    /// Get the underlying error message.
    pub fn description(&self) -> String {
        <Self as ResponseError>::description(self)
    }

    pub(super) fn to_error(&self) -> HttpResponse {
        ResponseBuilder::new(self.status_code).body(self.to_json())
    }

    pub(super) fn to_json(&self) -> Value {
        json!({
            "message": self.message,
            "statusCode": self.status_code.as_u16(),
            "success": false
        })
    }
}

impl ResponseError for Error {
    fn error_response(&self, req: HttpRequest) -> HttpResponse {
        let mut res = self.to_error();
        accept_json(&req, res.0.headers_mut());
        res
    }

    fn status_code(&self) -> StatusCode {
        self.status_code
    }

    fn description(&self) -> String {
        self.message.clone()
    }
}

pub fn accept_json(req: &HttpRequest, headers: &mut Headers) {
    if let Some(accept) = req.headers().get(&ACCEPT) {
        if accept.contains(STAR_STAR.essence_str()) || accept.contains(ContentType::json().as_str())
        {
            _ = headers.set(
                CONTENT_TYPE.as_str(),
                ContentType::json().to_header_value().to_str().unwrap(),
            );
        }
    }
}

impl From<Error> for worker::Error {
    fn from(err: Error) -> Self {
        Self::Json((err.to_json().to_string(), err.status_code.into()))
    }
}

impl From<serde_qs::Error> for Error {
    fn from(err: serde_qs::Error) -> Self {
        Self {
            message: err.description(),
            status_code: err.status_code(),
            cause: ErrorCause::Query,
        }
    }
}

impl From<worker::Error> for Error {
    fn from(err: worker::Error) -> Self {
        Self {
            message: err.description(),
            status_code: err.status_code(),
            cause: ErrorCause::Worker(err),
        }
    }
}

impl From<serde::de::value::Error> for Error {
    fn from(err: serde::de::value::Error) -> Self {
        Self {
            message: err.description(),
            status_code: err.status_code(),
            cause: ErrorCause::Query,
        }
    }
}

impl From<ToStrError> for Error {
    fn from(err: ToStrError) -> Self {
        Self {
            message: err.description(),
            status_code: err.status_code(),
            cause: ErrorCause::Header,
        }
    }
}