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

improve error messages for network connection failures #232

Merged
merged 2 commits into from
Sep 29, 2021
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
4 changes: 2 additions & 2 deletions src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ pub enum ClientError {
ParcelAlreadyExists,
/// The error returned when the request is invalid. Contains the underlying HTTP status code and
/// any message returned from the API
#[error("Invalid request (status code {status_code:?}): {message:?}")]
#[error("Invalid request (status code {status_code:?}): {}", .message.clone().unwrap_or_else(|| "unknown error".to_owned()))]
InvalidRequest {
status_code: reqwest::StatusCode,
message: Option<String>,
},
/// A server error was encountered. Contains an optional message from the server
#[error("Server has encountered an error: {0:?}")]
#[error("Error contacting server: {}", .0.clone().unwrap_or_else(||"Protocol error. Verify the Bindle URL".to_owned()))]
ServerError(Option<String>),
/// Invalid credentials were used or user does not have access to the requested resource. This
/// is only valid if the server supports authentication and/or permissions
Expand Down
4 changes: 4 additions & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ async fn unwrap_status(
(StatusCode::CONFLICT, Endpoint::Invoice) => Err(ClientError::InvoiceAlreadyExists),
(StatusCode::CONFLICT, Endpoint::Parcel) => Err(ClientError::ParcelAlreadyExists),
(StatusCode::UNAUTHORIZED, _) => Err(ClientError::Unauthorized),
(StatusCode::BAD_REQUEST, _) => Err(ClientError::ServerError(Some(
"The request could not be handled by the server. Verify your Bindle server URL"
.to_owned(),
))),
// You can't range match on u16 so we use a guard
(_, _) if resp.status().is_server_error() => {
Err(ClientError::ServerError(parse_error_from_body(resp).await))
Expand Down