diff options
Diffstat (limited to 'src/response/error.rs')
| -rw-r--r-- | src/response/error.rs | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/src/response/error.rs b/src/response/error.rs index c48489e..fb6a2fa 100644 --- a/src/response/error.rs +++ b/src/response/error.rs | |||
| @@ -8,34 +8,34 @@ use utoipa::ToSchema; | |||
| 8 | use crate::ApiError; | 8 | use crate::ApiError; |
| 9 | 9 | ||
| 10 | #[derive(Serialize, ToSchema)] | 10 | #[derive(Serialize, ToSchema)] |
| 11 | enum ErrorStatus { | 11 | #[serde(tag = "status")] |
| 12 | pub enum ErrorResponse { | ||
| 12 | #[serde(rename = "fail")] | 13 | #[serde(rename = "fail")] |
| 13 | Fail, | 14 | Fail { |
| 15 | #[schema(examples("SomeErrorKind", "NotAuthorized", "Database"))] | ||
| 16 | kind: String, | ||
| 17 | #[schema(examples("some error text"))] | ||
| 18 | message: String, | ||
| 19 | }, | ||
| 14 | #[serde(rename = "error")] | 20 | #[serde(rename = "error")] |
| 15 | Error, | 21 | Error { |
| 16 | } | 22 | #[schema(examples("SomeErrorKind", "NotAuthorized", "Database"))] |
| 17 | 23 | kind: String, | |
| 18 | #[derive(Serialize, ToSchema)] | 24 | #[schema(examples("some error text"))] |
| 19 | pub struct ErrorResponse { | 25 | message: String, |
| 20 | status: ErrorStatus, | 26 | }, |
| 21 | #[schema(examples("SomeErrorKind", "NotAuthorized", "Database"))] | ||
| 22 | kind: String, | ||
| 23 | #[schema(examples("some error text"))] | ||
| 24 | message: String, | ||
| 25 | } | 27 | } |
| 26 | 28 | ||
| 27 | impl ErrorResponse { | 29 | impl ErrorResponse { |
| 28 | pub fn fail(kind: impl Into<String>, message: impl Into<String>) -> Self { | 30 | pub fn fail(kind: impl Into<String>, message: impl Into<String>) -> Self { |
| 29 | Self { | 31 | Self::Fail { |
| 30 | status: ErrorStatus::Fail, | ||
| 31 | kind: kind.into(), | 32 | kind: kind.into(), |
| 32 | message: message.into(), | 33 | message: message.into(), |
| 33 | } | 34 | } |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | pub fn error(kind: impl Into<String>, message: impl Into<String>) -> Self { | 37 | pub fn error(kind: impl Into<String>, message: impl Into<String>) -> Self { |
| 37 | Self { | 38 | Self::Error { |
| 38 | status: ErrorStatus::Error, | ||
| 39 | kind: kind.into(), | 39 | kind: kind.into(), |
| 40 | message: message.into(), | 40 | message: message.into(), |
| 41 | } | 41 | } |
| @@ -45,9 +45,9 @@ impl ErrorResponse { | |||
| 45 | impl IntoResponse for ErrorResponse { | 45 | impl IntoResponse for ErrorResponse { |
| 46 | fn into_response(self) -> Response { | 46 | fn into_response(self) -> Response { |
| 47 | ( | 47 | ( |
| 48 | match self.status { | 48 | match self { |
| 49 | ErrorStatus::Fail => StatusCode::BAD_REQUEST, | 49 | Self::Fail { .. } => StatusCode::BAD_REQUEST, |
| 50 | ErrorStatus::Error => StatusCode::INTERNAL_SERVER_ERROR, | 50 | Self::Error { .. } => StatusCode::INTERNAL_SERVER_ERROR, |
| 51 | }, | 51 | }, |
| 52 | axum::Json(self), | 52 | axum::Json(self), |
| 53 | ) | 53 | ) |
| @@ -61,8 +61,14 @@ where | |||
| 61 | { | 61 | { |
| 62 | fn from(value: T) -> Self { | 62 | fn from(value: T) -> Self { |
| 63 | match value.into() { | 63 | match value.into() { |
| 64 | ApiError::Client(e) => Self::fail(e.kind(), e.into_message()), | 64 | ApiError::Client(e) => Self::Fail { |
| 65 | ApiError::Server(e) => Self::error(e.kind(), e.into_message()), | 65 | kind: e.kind(), |
| 66 | message: e.into_message(), | ||
| 67 | }, | ||
| 68 | ApiError::Server(e) => Self::Error { | ||
| 69 | kind: e.kind(), | ||
| 70 | message: e.into_message(), | ||
| 71 | }, | ||
| 66 | } | 72 | } |
| 67 | } | 73 | } |
| 68 | } | 74 | } |
