-
Notifications
You must be signed in to change notification settings - Fork 1
Improving errors #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
t3m8ch
wants to merge
12
commits into
main
Choose a base branch
from
t3m8ch/improving-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improving errors #21
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f7a543d
refactor: Improve errors
t3m8ch 2b43205
refactor: Rename errors in APIError
t3m8ch de5eb21
chore: Add comment for unwrap
t3m8ch 5df4454
fix: Clippy
t3m8ch 59eaca1
refactor: Create APIResult
t3m8ch e7b4284
refactor: Use ok_or
t3m8ch 71986fb
refactor: Change naming for errors
t3m8ch b6da5ef
refactor: Use Display trait for ErrorBody
t3m8ch 0f5019d
chore: Change comment
t3m8ch 82fde6d
Multiple changes to the error-handling
evgenymng f1ce2d7
fix: Fix typo in the display macro
evgenymng 0d869f9
refactor: Remove unnecessary logs
t3m8ch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,17 +1,2 @@ | ||
| pub mod handlers; | ||
| mod jwt; | ||
|
|
||
| #[derive(thiserror::Error, Debug)] | ||
| pub enum Error { | ||
| #[error("database error: {0}")] | ||
| Sqlx(#[from] sqlx::Error), | ||
| #[error("bcrypt error: {0}")] | ||
| Bcrypt(#[from] bcrypt::BcryptError), | ||
| #[error("jwt error: {0}")] | ||
| Jwt(#[from] jsonwebtoken::errors::Error), | ||
| #[error("auth error")] | ||
| Auth, | ||
| } | ||
| impl actix_web::error::ResponseError for Error {} | ||
|
|
||
| pub type Result<T> = std::result::Result<T, Error>; |
This file contains hidden or 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,49 @@ | ||
| use actix_web::{http::StatusCode, HttpResponse, ResponseError}; | ||
| use derive_more::{Display, Error}; | ||
| use serde::Serialize; | ||
|
|
||
| /// Represents the complete set of error codes the API may produce. | ||
| #[derive(Serialize, Clone, Debug, Display, Error)] | ||
| // NOTE(evgenymng): Intentionally not making it `Copy`, because we | ||
| // will probably need to store some additional information in those variants | ||
| // in the future. | ||
| pub enum ApiError { | ||
| #[display("wrong_credentials")] | ||
| WrongCredentials, | ||
| #[display("invalid_token")] | ||
| InvalidToken, | ||
| #[display("internal")] | ||
| Internal, | ||
| } | ||
|
|
||
| /// A convenient alias for the API handler's return type. | ||
| pub type ApiResult = Result<HttpResponse, ApiError>; | ||
|
|
||
| impl From<sqlx::Error> for ApiError { | ||
| fn from(_: sqlx::Error) -> Self { | ||
| ApiError::Internal | ||
| } | ||
| } | ||
|
|
||
| /// Structured error's body. If the API call fails with an error, | ||
| /// an object with this structure is sent as a response. | ||
| #[derive(Serialize)] | ||
| struct ErrorBody { | ||
| pub error: ApiError, | ||
| } | ||
|
|
||
| impl ResponseError for ApiError { | ||
| fn status_code(&self) -> StatusCode { | ||
| match self { | ||
| ApiError::WrongCredentials => StatusCode::UNAUTHORIZED, | ||
| ApiError::InvalidToken => StatusCode::UNAUTHORIZED, | ||
| ApiError::Internal => StatusCode::INTERNAL_SERVER_ERROR, | ||
| } | ||
| } | ||
|
|
||
| fn error_response(&self) -> HttpResponse { | ||
| HttpResponse::build(self.status_code()).json(ErrorBody { | ||
| error: self.clone(), | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ mod auth; | |
| mod config; | ||
| mod context; | ||
| mod db; | ||
| mod error; | ||
| mod models; | ||
| mod routes; | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.