-
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(endpoint_onchain_checkpoints): Error handling
- Loading branch information
Showing
6 changed files
with
74 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use axum::http::StatusCode; | ||
use axum::response::IntoResponse; | ||
use axum::Json; | ||
use serde_json::json; | ||
|
||
use crate::error::InfraError; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum CheckpointError { | ||
#[error("internal server error")] | ||
InternalServerError, | ||
#[error("invalid limit : {0}")] | ||
InvalidLimit(u64), | ||
#[error("no checkpoints found for requested pair")] | ||
NotFound(), | ||
} | ||
|
||
impl From<InfraError> for CheckpointError { | ||
fn from(error: InfraError) -> Self { | ||
match error { | ||
InfraError::InternalServerError => Self::InternalServerError, | ||
InfraError::NotFound => Self::NotFound(), | ||
InfraError::InvalidTimeStamp => Self::InternalServerError, | ||
} | ||
} | ||
} | ||
|
||
impl IntoResponse for CheckpointError { | ||
fn into_response(self) -> axum::response::Response { | ||
let (status, err_msg) = match self { | ||
Self::InvalidLimit(limit) => ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("Invalid Limit {}", limit), | ||
), | ||
Self::NotFound() => ( | ||
StatusCode::NOT_FOUND, | ||
String::from("No checkpoints found for requested pair"), | ||
), | ||
_ => ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
String::from("Internal server error"), | ||
), | ||
}; | ||
( | ||
status, | ||
Json( | ||
json!({"resource":"Checkpoint", "message": err_msg, "happened_at" : chrono::Utc::now() }), | ||
), | ||
) | ||
.into_response() | ||
} | ||
} |
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,3 +1,4 @@ | ||
pub mod checkpoint_error; | ||
pub mod currency; | ||
pub mod currency_error; | ||
pub mod entry; | ||
|
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