Skip to content
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

Add http errors 400, 408, 503, 500 #160

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading