Skip to main content

axum_error_sets/
code.rs

1use super::*;
2
3macro_rules! define_codes {
4    (
5        $(
6            $(#[$meta:meta])*
7            code $name:ident => $status:expr;
8        )*
9    ) => {
10        $(
11            $(#[$meta])*
12            #[derive(Debug, Clone, Copy, Default)]
13            pub struct $name<T = ()>(pub T);
14
15            impl<T, R, S> From<$name<R>> for ErrorSet<S, T>
16            where
17                R: Into<S>,
18                T: Contains<$name>,
19            {
20                fn from(err: $name<R>) -> Self {
21                    ErrorSet::new(err)
22                }
23            }
24
25            impl<T> StatusWrapper for $name<T> {
26                const STATUS_CODE: StatusCode = $status;
27
28                type Inner = T;
29                type Pure = $name;
30
31                fn into_inner(self) -> Self::Inner {
32                    self.0
33                }
34            }
35        )*
36    };
37}
38
39define_codes!(
40    /// 400 Bad Request
41    code BadRequest => StatusCode::BAD_REQUEST;
42
43    /// 401 Unauthorized
44    code Unauthorized => StatusCode::UNAUTHORIZED;
45
46    /// 402 Payment Required
47    code PaymentRequired => StatusCode::PAYMENT_REQUIRED;
48
49    /// 403 Forbidden
50    code Forbidden => StatusCode::FORBIDDEN;
51
52    /// 404 Not Found
53    code NotFound => StatusCode::NOT_FOUND;
54
55    /// 405 Method Not Allowed
56    code MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED;
57
58    /// 406 Not Acceptable
59    code NotAcceptable => StatusCode::NOT_ACCEPTABLE;
60
61    /// 407 Proxy Authentication Required
62    code ProxyAuthenticationRequired => StatusCode::PROXY_AUTHENTICATION_REQUIRED;
63
64    /// 408 Request Timeout
65    code RequestTimeout => StatusCode::REQUEST_TIMEOUT;
66
67    /// 409 Conflict
68    code Conflict => StatusCode::CONFLICT;
69
70    /// 410 Gone
71    code Gone => StatusCode::GONE;
72
73    /// 411 Length Required
74    code LengthRequired => StatusCode::LENGTH_REQUIRED;
75
76    /// 412 Precondition Failed
77    code PreconditionFailed => StatusCode::PRECONDITION_FAILED;
78
79    // /// 413 Content Too Large
80    // code ContentTooLarge => StatusCode::CONTENT_TOO_LARGE;
81
82    /// 414 URI Too Long
83    code UriTooLong => StatusCode::URI_TOO_LONG;
84
85    /// 415 Unsupported Media Type
86    code UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE;
87
88    /// 416 Range Not Satisfiable
89    code RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE;
90
91    /// 417 Expectation Failed
92    code ExpectationFailed => StatusCode::EXPECTATION_FAILED;
93
94    /// 418 I'm a teapot
95    code ImATeapot => StatusCode::IM_A_TEAPOT;
96
97    /// 421 Misdirected Request
98    code MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST;
99
100    /// 422 Unprocessable Entity
101    code UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY;
102
103    /// 423 Locked
104    code Locked => StatusCode::LOCKED;
105
106    /// 424 Failed Dependency
107    code FailedDependency => StatusCode::FAILED_DEPENDENCY;
108
109    // /// 425 Too Early
110    // code TooEarly => StatusCode::TOO_EARLY;
111
112    /// 426 Upgrade Required
113    code UpgradeRequired => StatusCode::UPGRADE_REQUIRED;
114
115    /// 428 Precondition Required
116    code PreconditionRequired => StatusCode::PRECONDITION_REQUIRED;
117
118    /// 429 Too Many Requests
119    code TooManyRequests => StatusCode::TOO_MANY_REQUESTS;
120
121    /// 431 Request Header Fields Too Large
122    code RequestHeaderFieldsTooLarge => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE;
123
124    /// 451 Unavailable For Legal Reasons
125    code UnavailableForLegalReasons => StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS;
126
127    /// 500 Internal Server Error
128    code InternalServerError => StatusCode::INTERNAL_SERVER_ERROR;
129
130    /// 501 Not Implemented
131    code NotImplemented => StatusCode::NOT_IMPLEMENTED;
132
133    /// 502 Bad Gateway
134    code BadGateway => StatusCode::BAD_GATEWAY;
135
136    /// 503 Service Unavailable
137    code ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE;
138
139    /// 504 Gateway Timeout
140    code GatewayTimeout => StatusCode::GATEWAY_TIMEOUT;
141
142    /// 505 HTTP Version Not Supported
143    code HttpVersionNotSupported => StatusCode::HTTP_VERSION_NOT_SUPPORTED;
144
145    /// 506 Variant Also Negotiates
146    code VariantAlsoNegotiates => StatusCode::VARIANT_ALSO_NEGOTIATES;
147
148    /// 507 Insufficient Storage
149    code InsufficientStorage => StatusCode::INSUFFICIENT_STORAGE;
150
151    /// 508 Loop Detected
152    code LoopDetected => StatusCode::LOOP_DETECTED;
153
154    /// 510 Not Extended
155    code NotExtended => StatusCode::NOT_EXTENDED;
156
157    /// 511 Network Authentication Required
158    code NetworkAuthenticationRequired => StatusCode::NETWORK_AUTHENTICATION_REQUIRED;
159);