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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::{
    collections::{BTreeMap, HashSet},
    convert::Infallible,
    error::Error,
    fmt,
    string::FromUtf8Error,
};

use http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
use mime::TEXT_PLAIN_UTF_8;

use crate::{
    error::BoxError,
    media_type::MultiResponseMediaType,
    openapi::{self, Components},
    response::Response,
};

pub trait ResponseError: Error + Send + Sync + Sized + 'static {
    fn as_status(&self) -> StatusCode;

    fn status_codes() -> HashSet<StatusCode>;

    fn as_response(&self) -> Response {
        Response::builder()
            .status(self.as_status())
            .header(
                CONTENT_TYPE,
                HeaderValue::from_static(TEXT_PLAIN_UTF_8.as_ref()),
            )
            .body(self.to_string().into())
            .unwrap()
    }

    fn responses(components: &mut Components) -> BTreeMap<StatusCode, openapi::Response> {
        Self::status_codes()
            .into_iter()
            .map(|status| {
                (
                    status,
                    openapi::Response {
                        description: status.canonical_reason().unwrap_or_default().to_string(),
                        content: <String as MultiResponseMediaType>::content(components),
                        ..Default::default()
                    },
                )
            })
            .collect()
    }

    #[doc(hidden)]
    fn inner(self) -> BoxError {
        Box::new(self)
    }

    #[doc(hidden)]
    fn wrappers(&self, type_names: &mut Vec<&'static str>) {
        type_names.push(std::any::type_name::<Self>());
    }
}

impl ResponseError for Infallible {
    fn as_status(&self) -> StatusCode {
        match *self {}
    }

    fn status_codes() -> HashSet<StatusCode> {
        HashSet::new()
    }

    fn as_response(&self) -> Response {
        match *self {}
    }

    fn responses(_: &mut Components) -> BTreeMap<StatusCode, openapi::Response> {
        BTreeMap::new()
    }
}

#[derive(Debug)]
pub struct RequestBodyLimitError {
    pub actual: Option<usize>,
    pub expected: usize,
}

impl fmt::Display for RequestBodyLimitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.actual {
            Some(actual) => {
                write!(
                    f,
                    "payload too large: expected `{}` but actual `{}`",
                    self.expected, actual
                )
            }
            None => {
                write!(
                    f,
                    "payload too large (no content length): expected `{}`",
                    self.expected
                )
            }
        }
    }
}

impl Error for RequestBodyLimitError {}

impl ResponseError for RequestBodyLimitError {
    fn as_status(&self) -> StatusCode {
        StatusCode::PAYLOAD_TOO_LARGE
    }

    fn status_codes() -> HashSet<StatusCode> {
        [StatusCode::PAYLOAD_TOO_LARGE].into()
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ReadBytesError {
    #[error("{0}")]
    RequestBodyLimitError(#[from] RequestBodyLimitError),
    #[error("failed to read bytes from request body: {0}")]
    UnknownBodyError(#[from] BoxError),
}

impl ResponseError for ReadBytesError {
    fn as_status(&self) -> StatusCode {
        match self {
            ReadBytesError::RequestBodyLimitError(e) => e.as_status(),
            ReadBytesError::UnknownBodyError(_) => StatusCode::BAD_REQUEST,
        }
    }

    fn status_codes() -> HashSet<StatusCode> {
        let mut status_codes = RequestBodyLimitError::status_codes();
        status_codes.insert(StatusCode::BAD_REQUEST);
        status_codes
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ReadStringError {
    #[error("{0}")]
    ReadBytes(#[from] ReadBytesError),
    #[error("failed to convert bytes to string: {0}")]
    InvalidUtf8(#[from] FromUtf8Error),
}

impl ResponseError for ReadStringError {
    fn as_status(&self) -> StatusCode {
        match self {
            ReadStringError::ReadBytes(e) => e.as_status(),
            ReadStringError::InvalidUtf8(_) => StatusCode::BAD_REQUEST,
        }
    }

    fn status_codes() -> HashSet<StatusCode> {
        let mut status_codes = ReadBytesError::status_codes();
        status_codes.insert(StatusCode::BAD_REQUEST);
        status_codes
    }
}