-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
There was a problem hiding this 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())) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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::url
to 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> { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was from rustfmt?
There was a problem hiding this comment.
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: ")?; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
77aebd6
to
bdfdaa4
Compare
This makes it it easy to turn error responses into error results.
bdfdaa4
to
184044a
Compare
I rebased this into master here 855e661 Thanks! |
This makes it it easy to turn error responses into error results.
This is inspired by python requests'
raise_for_status
.