Skip to main content

axum_error_sets/
result_ext.rs

1//! Contains all [`StatusWrapper`] types for the HTTP status codes defined in the `http` crate.
2
3use crate::{ErrorSet, code::*};
4use type_sets::SupersetOf;
5
6/// Extension trait for [`Result`] to convert it into a [`Result`] with an [`ApiError`].
7pub trait StatusResultExt {
8    /// The `Ok` type of the [`Result`].
9    type Ok;
10
11    /// The `Err` type of the [`Result`].
12    type Err;
13
14    /// Converts the error into a `400 Bad Request` API error.
15    fn into_bad_request(self) -> Result<Self::Ok, BadRequest<Self::Err>>;
16
17    /// Converts the error into a `401 Unauthorized` API error.
18    fn into_unauthorized(self) -> Result<Self::Ok, Unauthorized<Self::Err>>;
19
20    /// Converts the error into a `403 Forbidden` API error.
21    fn into_forbidden(self) -> Result<Self::Ok, Forbidden<Self::Err>>;
22
23    /// Converts the error into a `404 Not Found` API error.
24    fn into_not_found(self) -> Result<Self::Ok, NotFound<Self::Err>>;
25
26    /// Converts the error into a `409 Conflict` API error.
27    fn into_conflict(self) -> Result<Self::Ok, Conflict<Self::Err>>;
28
29    /// Converts the error into a `422 Unprocessable Entity` API error.
30    fn into_unprocessable_entity(self) -> Result<Self::Ok, UnprocessableEntity<Self::Err>>;
31
32    /// Converts the error into a `429 Too Many Requests` API error.
33    fn into_too_many_requests(self) -> Result<Self::Ok, TooManyRequests<Self::Err>>;
34
35    /// Converts the error into a `500 Internal Server Error` API error.
36    fn into_internal(self) -> Result<Self::Ok, InternalServerError<Self::Err>>;
37
38    /// Converts the error into a `502 Bad Gateway` API error.
39    fn into_bad_gateway(self) -> Result<Self::Ok, BadGateway<Self::Err>>;
40
41    /// Converts the error into a `503 Service Unavailable` API error.
42    fn into_service_unavailable(self) -> Result<Self::Ok, ServiceUnavailable<Self::Err>>;
43
44    /// Converts the error into a `504 Gateway Timeout` API error.
45    fn into_gateway_timeout(self) -> Result<Self::Ok, GatewayTimeout<Self::Err>>;
46}
47
48impl<T, E> StatusResultExt for Result<T, E> {
49    type Ok = T;
50    type Err = E;
51
52    fn into_bad_request(self) -> Result<T, BadRequest<E>> {
53        self.map_err(BadRequest)
54    }
55
56    fn into_unauthorized(self) -> Result<T, Unauthorized<E>> {
57        self.map_err(Unauthorized)
58    }
59
60    fn into_forbidden(self) -> Result<T, Forbidden<E>> {
61        self.map_err(Forbidden)
62    }
63
64    fn into_not_found(self) -> Result<T, NotFound<E>> {
65        self.map_err(NotFound)
66    }
67
68    fn into_conflict(self) -> Result<T, Conflict<E>> {
69        self.map_err(Conflict)
70    }
71
72    fn into_unprocessable_entity(self) -> Result<T, UnprocessableEntity<E>> {
73        self.map_err(UnprocessableEntity)
74    }
75
76    fn into_too_many_requests(self) -> Result<T, TooManyRequests<E>> {
77        self.map_err(TooManyRequests)
78    }
79
80    fn into_internal(self) -> Result<T, InternalServerError<E>> {
81        self.map_err(InternalServerError)
82    }
83
84    fn into_bad_gateway(self) -> Result<T, BadGateway<E>> {
85        self.map_err(BadGateway)
86    }
87
88    fn into_service_unavailable(self) -> Result<T, ServiceUnavailable<E>> {
89        self.map_err(ServiceUnavailable)
90    }
91
92    fn into_gateway_timeout(self) -> Result<T, GatewayTimeout<E>> {
93        self.map_err(GatewayTimeout)
94    }
95}
96
97/// Extension trait for results, to convert errorsets into supersets.
98pub trait ResultSetExt {
99    type Ok;
100    type Err;
101    type Value;
102
103    fn into_superset<E>(self) -> Result<Self::Ok, ErrorSet<Self::Value, E>>
104    where
105        E: SupersetOf<Self::Err>;
106}
107
108impl<T, E, V> ResultSetExt for Result<T, ErrorSet<V, E>> {
109    type Ok = T;
110    type Err = E;
111    type Value = V;
112
113    fn into_superset<E2>(self) -> Result<T, ErrorSet<V, E2>>
114    where
115        E2: SupersetOf<E>,
116    {
117        self.map_err(|e| e.into_superset())
118    }
119}