poem_openapi/
error.rs

1//! Some common error types.
2
3use poem::{error::ResponseError, http::StatusCode};
4use thiserror::Error;
5
6/// Parameter error.
7#[derive(Debug, Error)]
8#[error("failed to parse parameter `{name}`: {reason}")]
9pub struct ParseParamError {
10    /// The name of the parameter.
11    pub name: &'static str,
12
13    /// The reason for the error.
14    pub reason: String,
15}
16
17impl ResponseError for ParseParamError {
18    fn status(&self) -> StatusCode {
19        StatusCode::BAD_REQUEST
20    }
21}
22
23/// Parameter error.
24#[derive(Debug, Error)]
25#[error("failed to parse path `{name}`: {reason}")]
26pub struct ParsePathError {
27    /// The name of the parameter.
28    pub name: &'static str,
29
30    /// The reason for the error.
31    pub reason: String,
32}
33
34impl ResponseError for ParsePathError {
35    fn status(&self) -> StatusCode {
36        StatusCode::BAD_REQUEST
37    }
38}
39
40/// Parse request payload error.
41#[derive(Debug, Error)]
42#[error("parse request payload error: {reason}")]
43pub struct ParseRequestPayloadError {
44    /// The reason for the error.
45    pub reason: String,
46}
47
48impl ResponseError for ParseRequestPayloadError {
49    fn status(&self) -> StatusCode {
50        StatusCode::BAD_REQUEST
51    }
52}
53
54/// Parse multipart error.
55#[derive(Debug, Error)]
56#[error("parse multipart error: {reason}")]
57pub struct ParseMultipartError {
58    /// The reason for the error.
59    pub reason: String,
60}
61
62impl ResponseError for ParseMultipartError {
63    fn status(&self) -> StatusCode {
64        StatusCode::BAD_REQUEST
65    }
66}
67
68/// Content type error.
69#[derive(Debug, Error)]
70pub enum ContentTypeError {
71    /// Not supported.
72    #[error("the `Content-Type` requested by the client is not supported: {content_type}")]
73    NotSupported {
74        /// The `Content-Type` header requested by the client.
75        content_type: String,
76    },
77
78    /// Expect content type header.
79    #[error("the client request does not include the `Content-Type` header")]
80    ExpectContentType,
81}
82
83impl ResponseError for ContentTypeError {
84    fn status(&self) -> StatusCode {
85        StatusCode::UNSUPPORTED_MEDIA_TYPE
86    }
87}
88
89/// Authorization error.
90#[derive(Debug, Error)]
91#[error("authorization error")]
92pub struct AuthorizationError;
93
94impl ResponseError for AuthorizationError {
95    fn status(&self) -> StatusCode {
96        StatusCode::UNAUTHORIZED
97    }
98}