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
use poem::http::StatusCode;
use thiserror::Error;
use crate::poem::error::{BadRequest, MethodNotAllowed};
#[derive(Debug, Error, Clone, Eq, PartialEq)]
pub enum ParseRequestError {
#[error("failed to parse param `{name}`: {reason}")]
ParseParam {
name: &'static str,
reason: String,
},
#[error("failed to parse request body: {reason}")]
ParseRequestBody {
reason: String,
},
#[error("the content type `{content_type}` is not supported.")]
ContentTypeNotSupported {
content_type: String,
},
#[error("expect a `Content-Type` header.")]
ExpectContentType,
#[error("poem extract error: {0}")]
Extractor(String),
#[error("authorization error")]
Authorization,
}
impl From<ParseRequestError> for poem::Error {
fn from(err: ParseRequestError) -> Self {
match &err {
ParseRequestError::ParseParam { .. } => BadRequest(err),
ParseRequestError::ParseRequestBody { .. } => BadRequest(err),
ParseRequestError::ContentTypeNotSupported { .. } => MethodNotAllowed(err),
ParseRequestError::ExpectContentType => MethodNotAllowed(err),
ParseRequestError::Extractor(_) => BadRequest(err),
ParseRequestError::Authorization => poem::Error::new(StatusCode::UNAUTHORIZED),
}
}
}