-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
156 additions
and
65 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use thiserror::Error; | ||
use actix_web::error::ResponseError; | ||
use actix_web::http::StatusCode; | ||
use actix_web::HttpResponse; | ||
use sqlx::Error as SqlxError; | ||
use serde_json::json; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum AppError { | ||
#[error("The requested resource was not found in the database")] | ||
NotFound, | ||
|
||
#[error("Internal Server Error")] | ||
InternalServerError, | ||
} | ||
|
||
impl ResponseError for AppError { | ||
fn error_response(&self) -> HttpResponse { | ||
match self { | ||
AppError::NotFound => { | ||
HttpResponse::NotFound().json(json!({ | ||
"message": self.to_string() | ||
})) | ||
} | ||
AppError::InternalServerError => { | ||
HttpResponse::InternalServerError().json(json!({ | ||
"message": self.to_string() | ||
})) | ||
} | ||
} | ||
} | ||
fn status_code(&self) -> StatusCode { | ||
match *self { | ||
AppError::NotFound => { | ||
StatusCode::NOT_FOUND | ||
} | ||
AppError::InternalServerError => { | ||
StatusCode::INTERNAL_SERVER_ERROR | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<SqlxError> for AppError { | ||
fn from(err: SqlxError) -> Self { | ||
match err { | ||
SqlxError::RowNotFound | SqlxError::ColumnNotFound(_) => { | ||
AppError::NotFound | ||
} | ||
_ => { | ||
AppError::InternalServerError | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters