-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api error codes and redacted secrets (#6)
<!-- Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. --> Provides `Secret` struct to keep sensitive data hidden, also Error Codes support. Makes sure core logic has correct error handling by enumerating errors from different cases and providing explicit Status Codes for each error. Makes sure routes matches the ones under `account`, refer: https://github.com/commune-os/commune-server/blob/df257384c8daede9fdf0e93c51af3c6444e15745/app/routes.go#L122-L164
- Loading branch information
1 parent
88fd973
commit 84021a8
Showing
21 changed files
with
425 additions
and
46 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use http::StatusCode; | ||
use thiserror::Error; | ||
|
||
use crate::user::error::UserErrorCode; | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
pub trait HttpStatusCode { | ||
fn status_code(&self) -> StatusCode; | ||
fn error_code(&self) -> &'static str; | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("User Error. {0}")] | ||
User(UserErrorCode), | ||
#[error("Unknown Error Occured")] | ||
Unknown, | ||
} | ||
|
||
impl From<UserErrorCode> for Error { | ||
fn from(err: UserErrorCode) -> Self { | ||
Error::User(err) | ||
} | ||
} | ||
|
||
impl HttpStatusCode for Error { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
Error::User(err) => err.status_code(), | ||
Error::Unknown => StatusCode::INTERNAL_SERVER_ERROR, | ||
} | ||
} | ||
|
||
fn error_code(&self) -> &'static str { | ||
match self { | ||
Error::User(err) => err.error_code(), | ||
Error::Unknown => "UNKNOWN_ERROR", | ||
} | ||
} | ||
} |
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,29 @@ | ||
use http::StatusCode; | ||
use thiserror::Error; | ||
use validator::ValidationErrors; | ||
|
||
use crate::error::HttpStatusCode; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum UserErrorCode { | ||
#[error("Vaildation error. {0}")] | ||
ValidationError(#[from] ValidationErrors), | ||
#[error("The username {0} is already taken")] | ||
UsernameTaken(String), | ||
} | ||
|
||
impl HttpStatusCode for UserErrorCode { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
UserErrorCode::ValidationError(_) => StatusCode::BAD_REQUEST, | ||
UserErrorCode::UsernameTaken(_) => StatusCode::CONFLICT, | ||
} | ||
} | ||
|
||
fn error_code(&self) -> &'static str { | ||
match self { | ||
UserErrorCode::ValidationError(_) => "VALIDATION_ERROR", | ||
UserErrorCode::UsernameTaken(_) => "USERNAME_TAKEN", | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod error; | ||
pub mod model; | ||
pub mod service; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod secret; | ||
pub mod time; |
Oops, something went wrong.