Skip to content

Commit

Permalink
feat: added http errors 400, 408, 503, 500 (#160)
Browse files Browse the repository at this point in the history
* Add http errors 400, 408, 503, 500

* fmt

* Updated error messages

---------

Co-authored-by: akorchyn <[email protected]>
Co-authored-by: Artur Yurii Korchynskyi <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent 81aa836 commit 942584e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ pub enum JsonRpcServerResponseStatusError {
/// The RPC client exceeds the rate limit by sending too many requests.
#[error("this client has exceeded the rate limit")]
TooManyRequests,
#[error("the server returned status code 400 - bad request")]
BadRequest,
#[error("the request failed with timeout error")]
TimeoutError,
#[error("the server is unavailable")]
ServiceUnavailable,
/// The RPC server returned a non-200 status code.
#[error("the server returned a non-OK (200) status code: [{status}]")]
Unexpected { status: reqwest::StatusCode },
Expand Down
42 changes: 30 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,37 @@ impl JsonRpcClient {
match response.status() {
reqwest::StatusCode::OK => {}
non_ok_status => {
return Err(JsonRpcError::ServerError(
JsonRpcServerError::ResponseStatusError(match non_ok_status {
reqwest::StatusCode::UNAUTHORIZED => {
JsonRpcServerResponseStatusError::Unauthorized
return Err(JsonRpcError::ServerError(match non_ok_status {
reqwest::StatusCode::UNAUTHORIZED => JsonRpcServerError::ResponseStatusError(
JsonRpcServerResponseStatusError::Unauthorized,
),
reqwest::StatusCode::TOO_MANY_REQUESTS => {
JsonRpcServerError::ResponseStatusError(
JsonRpcServerResponseStatusError::TooManyRequests,
)
}
reqwest::StatusCode::BAD_REQUEST => JsonRpcServerError::ResponseStatusError(
JsonRpcServerResponseStatusError::BadRequest,
),
reqwest::StatusCode::INTERNAL_SERVER_ERROR => {
JsonRpcServerError::InternalError {
info: Some(String::from("Internal server error")),
}
reqwest::StatusCode::TOO_MANY_REQUESTS => {
JsonRpcServerResponseStatusError::TooManyRequests
}
unexpected => {
JsonRpcServerResponseStatusError::Unexpected { status: unexpected }
}
}),
));
}
reqwest::StatusCode::SERVICE_UNAVAILABLE => {
JsonRpcServerError::ResponseStatusError(
JsonRpcServerResponseStatusError::ServiceUnavailable,
)
}
reqwest::StatusCode::REQUEST_TIMEOUT => {
JsonRpcServerError::ResponseStatusError(
JsonRpcServerResponseStatusError::TimeoutError,
)
}
unexpected => JsonRpcServerError::ResponseStatusError(
JsonRpcServerResponseStatusError::Unexpected { status: unexpected },
),
}));
}
}
let response_payload = response.bytes().await.map_err(|err| {
Expand Down

0 comments on commit 942584e

Please sign in to comment.