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 error_for_status. #98

Closed
wants to merge 1 commit into from

Conversation

tomprince
Copy link
Contributor

This makes it it easy to turn error responses into error results.

This is inspired by python requests' raise_for_status.

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dig the idea, and thanks for the code!

As I was leaving comments, I started to notice a pattern in my thinking: should we differentiate between 400s and 500s in the Error, or should it simply be a status code error, like in python-requests?

Err(::error::client_error(self.url().clone(), *self.status()))
}
StatusClass::ServerError => {
Err(::error::server_error(self.url().clone(), *self.status()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is consuming the Response, is there a way we can just take the Url, instead of cloning it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. At the very least, we'd need to duplicate the body of Response::urlto get an owned version. But when I try that, I can't extract the url from the hyper::client::response::Response since that implements Drop.

src/error.rs Outdated

/// Returns the status code, if the error was generated from a response.
#[inline]
pub fn status(&self) -> Option<&::hyper::status::StatusCode> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the StatusCode type is basically just a u32 (1 byte for tag, 2 for Unregistered(u16), 1 for padding), and it's Copy, may as well return it by value instead of by ref.

This is something that will be changed in Response::status() -> StatusCode as well, for the next breaking change round.

src/error.rs Outdated
@@ -58,11 +60,38 @@ impl Error {
#[inline]
pub fn is_redirect(&self) -> bool {
match self.kind {
Kind::TooManyRedirects |
Kind::RedirectLoop => true,
Kind::TooManyRedirects | Kind::RedirectLoop => true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was from rustfmt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume so.

fmt::Display::fmt(code, f)
}
Kind::ServerError(ref code) => {
f.write_str("Server Error: ")?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm imagining what the message would look like:

https://hyper.rs/not-here: Client Error: 404 Not Found

Is there value in the middle part, Client Error: or Server Error: , I wonder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I suspect that this will be shown to end-users who might not be familiar with the different HTTP status codes. (Note that I was just copying the python version here

src/error.rs Outdated

/// Returns true if the error is from a request returning a 4xx error.
#[inline]
pub fn is_client_error(&self) -> bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there'd be confusing that this means a status.is_client_error(), and not an error from reqwest::Client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used reqwest enough to answer this.

@tomprince tomprince force-pushed the error-for-status branch 3 times, most recently from 77aebd6 to bdfdaa4 Compare May 25, 2017 19:49
This makes it it easy to turn error responses into error
results.
@seanmonstar
Copy link
Owner

I rebased this into master here 855e661

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants