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
//! Some common error types.

use poem::{error::ResponseError, http::StatusCode};
use thiserror::Error;

/// Parameter error.
#[derive(Debug, Error)]
#[error("failed to parse parameter `{name}`: {reason}")]
pub struct ParseParamError {
    /// The name of the parameter.
    pub name: &'static str,

    /// The reason for the error.
    pub reason: String,
}

impl ResponseError for ParseParamError {
    fn status(&self) -> StatusCode {
        StatusCode::BAD_REQUEST
    }
}

/// Parameter error.
#[derive(Debug, Error)]
#[error("failed to parse path `{name}`: {reason}")]
pub struct ParsePathError {
    /// The name of the parameter.
    pub name: &'static str,

    /// The reason for the error.
    pub reason: String,
}

impl ResponseError for ParsePathError {
    fn status(&self) -> StatusCode {
        StatusCode::BAD_REQUEST
    }
}

/// Parse request payload error.
#[derive(Debug, Error)]
#[error("parse request payload error: {reason}")]
pub struct ParseRequestPayloadError {
    /// The reason for the error.
    pub reason: String,
}

impl ResponseError for ParseRequestPayloadError {
    fn status(&self) -> StatusCode {
        StatusCode::BAD_REQUEST
    }
}

/// Parse multipart error.
#[derive(Debug, Error)]
#[error("parse multipart error: {reason}")]
pub struct ParseMultipartError {
    /// The reason for the error.
    pub reason: String,
}

impl ResponseError for ParseMultipartError {
    fn status(&self) -> StatusCode {
        StatusCode::BAD_REQUEST
    }
}

/// Content type error.
#[derive(Debug, Error)]
pub enum ContentTypeError {
    /// Not supported.
    #[error("the `Content-Type` requested by the client is not supported: {content_type}")]
    NotSupported {
        /// The `Content-Type` header requested by the client.
        content_type: String,
    },

    /// Expect content type header.
    #[error("the client request does not include the `Content-Type` header")]
    ExpectContentType,
}

impl ResponseError for ContentTypeError {
    fn status(&self) -> StatusCode {
        StatusCode::UNSUPPORTED_MEDIA_TYPE
    }
}

/// Authorization error.
#[derive(Debug, Error)]
#[error("authorization error")]
pub struct AuthorizationError;

impl ResponseError for AuthorizationError {
    fn status(&self) -> StatusCode {
        StatusCode::UNAUTHORIZED
    }
}