|
@@ -0,0 +1,43 @@
|
|
|
|
|
+use actix_web::{HttpResponse, ResponseError, http::StatusCode};
|
|
|
|
|
+use thiserror::ErrorInfo;
|
|
|
|
|
+use serde::Serialize;
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Serialize)]
|
|
|
|
|
+struct ErrorBody {
|
|
|
|
|
+ error: ErrorInfo
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Serialize)]
|
|
|
|
|
+struct ErrorInfo {
|
|
|
|
|
+ code: u16,
|
|
|
|
|
+ message: String
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Error)]
|
|
|
|
|
+pub enum AppError {
|
|
|
|
|
+ #[error("Internal Server Error")]
|
|
|
|
|
+ InternalError(String)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl ResponseError for AppError {
|
|
|
|
|
+ fn status_code(&self) -> StatusCode {
|
|
|
|
|
+ match self {
|
|
|
|
|
+ AppError::InternalError => StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn error_response(&self) -> HttpResponse {
|
|
|
|
|
+ match self {
|
|
|
|
|
+ AppError::InternalError(s) => {eprintln!("Internal Error: {}", s)}
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let body = ErrorBody {
|
|
|
|
|
+ error: ErrorInfo {
|
|
|
|
|
+ code: self.status_code().as_u16(),
|
|
|
|
|
+ message: self.to_string()
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse::build(self.status_code()).json(body)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|