|
@@ -0,0 +1,38 @@
|
|
|
|
|
+use actix_web::{HttpResponse, ResponseError, http::StatusCode};
|
|
|
|
|
+use thiserror::Error;
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Serialize)]
|
|
|
|
|
+struct ErrorBody {
|
|
|
|
|
+ error: ErrorInfo
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Serialize)]
|
|
|
|
|
+struct ErrorInfo {
|
|
|
|
|
+ code: u16,
|
|
|
|
|
+ message: String
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Error, Debug)]
|
|
|
|
|
+pub enum HttpError {
|
|
|
|
|
+ #[error("{0}")]
|
|
|
|
|
+ JsonDeserializationError(String)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl ResponseError for HttpError {
|
|
|
|
|
+ fn status_code(&self) -> StatusCode {
|
|
|
|
|
+ match self {
|
|
|
|
|
+ HttpError::JsonDeserializationError(_) => StatusCode::BAD_REQUEST
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn error_response(&self) -> HttpResponse {
|
|
|
|
|
+ let body = ErrorBody {
|
|
|
|
|
+ error: ErrorInfo {
|
|
|
|
|
+ code: self.status_code().as_u16(),
|
|
|
|
|
+ message: self.to_string()
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse::build(self.status_code()).json(body)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|