1pub use actix_http::error::{ContentTypeError, DispatchError, HttpError, ParseError, PayloadError};
9use derive_more::{Display, Error, From};
10use serde_json::error::Error as JsonError;
11use serde_urlencoded::{de::Error as FormDeError, ser::Error as FormError};
12use url::ParseError as UrlParseError;
13
14use crate::http::StatusCode;
15
16#[allow(clippy::module_inception)]
17mod error;
18mod internal;
19mod macros;
20mod response_error;
21
22pub(crate) use self::macros::{downcast_dyn, downcast_get_type_id};
23pub use self::{error::Error, internal::*, response_error::ResponseError};
24pub use crate::types::EitherExtractError;
25
26pub type Result<T, E = Error> = std::result::Result<T, E>;
30
31#[derive(Debug, Display, Error)]
33#[display("Blocking thread pool is shut down unexpectedly")]
34#[non_exhaustive]
35pub struct BlockingError;
36
37impl ResponseError for crate::error::BlockingError {}
38
39#[derive(Debug, PartialEq, Eq, Display, Error, From)]
41#[non_exhaustive]
42pub enum UrlGenerationError {
43 #[display("Resource not found")]
45 ResourceNotFound,
46
47 #[display("Not all URL parameters covered")]
49 NotEnoughElements,
50
51 #[display("{}", _0)]
53 ParseError(UrlParseError),
54}
55
56impl ResponseError for UrlGenerationError {}
57
58#[derive(Debug, Display, Error, From)]
60#[non_exhaustive]
61pub enum UrlencodedError {
62 #[display("Can not decode chunked transfer encoding")]
64 Chunked,
65
66 #[display(
68 "URL-encoded payload is larger ({} bytes) than allowed (limit: {} bytes)",
69 size,
70 limit
71 )]
72 Overflow { size: usize, limit: usize },
73
74 #[display("URL encoded payload is larger than allowed (limit: {} bytes)", limit)]
76 OverflowUnknownLength { limit: usize },
77
78 #[display("Payload size is now known")]
80 UnknownLength,
81
82 #[display("Content type error")]
84 ContentType,
85
86 #[display("Parse error: {}", _0)]
88 Parse(FormDeError),
89
90 #[display("Encoding error")]
92 Encoding,
93
94 #[display("Serialize error: {}", _0)]
96 Serialize(FormError),
97
98 #[display("Error that occur during reading payload: {}", _0)]
100 Payload(PayloadError),
101}
102
103impl ResponseError for UrlencodedError {
104 fn status_code(&self) -> StatusCode {
105 match self {
106 Self::Overflow { .. } | Self::OverflowUnknownLength { .. } => {
107 StatusCode::PAYLOAD_TOO_LARGE
108 }
109 Self::UnknownLength => StatusCode::LENGTH_REQUIRED,
110 Self::ContentType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
111 Self::Payload(err) => err.status_code(),
112 _ => StatusCode::BAD_REQUEST,
113 }
114 }
115}
116
117#[derive(Debug, Display, Error)]
119#[non_exhaustive]
120pub enum JsonPayloadError {
121 #[display(
123 "JSON payload ({} bytes) is larger than allowed (limit: {} bytes)",
124 length,
125 limit
126 )]
127 OverflowKnownLength { length: usize, limit: usize },
128
129 #[display("JSON payload has exceeded limit ({} bytes)", limit)]
131 Overflow { limit: usize },
132
133 #[display("Content type error")]
135 ContentType,
136
137 #[display("Json deserialize error: {}", _0)]
139 Deserialize(JsonError),
140
141 #[display("Json serialize error: {}", _0)]
143 Serialize(JsonError),
144
145 #[display("Error that occur during reading payload: {}", _0)]
147 Payload(PayloadError),
148}
149
150impl_more::impl_enum_from!(PayloadError => JsonPayloadError::Payload);
151
152impl ResponseError for JsonPayloadError {
153 fn status_code(&self) -> StatusCode {
154 match self {
155 Self::OverflowKnownLength {
156 length: _,
157 limit: _,
158 } => StatusCode::PAYLOAD_TOO_LARGE,
159 Self::Overflow { limit: _ } => StatusCode::PAYLOAD_TOO_LARGE,
160 Self::Serialize(_) => StatusCode::INTERNAL_SERVER_ERROR,
161 Self::Payload(err) => err.status_code(),
162 _ => StatusCode::BAD_REQUEST,
163 }
164 }
165}
166
167#[derive(Debug, Display, Error)]
169#[non_exhaustive]
170pub enum PathError {
171 #[display("Path deserialize error: {}", _0)]
173 Deserialize(serde::de::value::Error),
174}
175
176impl ResponseError for PathError {
178 fn status_code(&self) -> StatusCode {
179 StatusCode::BAD_REQUEST
180 }
181}
182
183#[derive(Debug, Display, Error, From)]
185#[non_exhaustive]
186pub enum QueryPayloadError {
187 #[display("Query deserialize error: {}", _0)]
189 Deserialize(serde::de::value::Error),
190}
191
192impl ResponseError for QueryPayloadError {
193 fn status_code(&self) -> StatusCode {
194 StatusCode::BAD_REQUEST
195 }
196}
197
198#[derive(Debug, Display, Error, From)]
200#[non_exhaustive]
201pub enum ReadlinesError {
202 #[display("Encoding error")]
203 EncodingError,
205
206 #[display("Error that occur during reading payload: {}", _0)]
208 Payload(PayloadError),
209
210 #[display("Line limit exceeded")]
212 LimitOverflow,
213
214 #[display("Content-type error")]
216 ContentTypeError(ContentTypeError),
217}
218
219impl ResponseError for ReadlinesError {
220 fn status_code(&self) -> StatusCode {
221 match *self {
222 ReadlinesError::LimitOverflow => StatusCode::PAYLOAD_TOO_LARGE,
223 _ => StatusCode::BAD_REQUEST,
224 }
225 }
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231
232 #[test]
233 fn test_urlencoded_error() {
234 let resp = UrlencodedError::Overflow { size: 0, limit: 0 }.error_response();
235 assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
236 let resp = UrlencodedError::UnknownLength.error_response();
237 assert_eq!(resp.status(), StatusCode::LENGTH_REQUIRED);
238 let resp = UrlencodedError::ContentType.error_response();
239 assert_eq!(resp.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
240 }
241
242 #[test]
243 fn test_json_payload_error() {
244 let resp = JsonPayloadError::OverflowKnownLength {
245 length: 0,
246 limit: 0,
247 }
248 .error_response();
249 assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
250 let resp = JsonPayloadError::Overflow { limit: 0 }.error_response();
251 assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
252 let resp = JsonPayloadError::ContentType.error_response();
253 assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
254 }
255
256 #[test]
257 fn test_query_payload_error() {
258 let resp = QueryPayloadError::Deserialize(
259 serde_urlencoded::from_str::<i32>("bad query").unwrap_err(),
260 )
261 .error_response();
262 assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
263 }
264
265 #[test]
266 fn test_readlines_error() {
267 let resp = ReadlinesError::LimitOverflow.error_response();
268 assert_eq!(resp.status(), StatusCode::PAYLOAD_TOO_LARGE);
269 let resp = ReadlinesError::EncodingError.error_response();
270 assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
271 }
272}