Skip to content

Commit

Permalink
combine ClientError and ServerError into Status kind
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Apr 25, 2019
1 parent 45484d8 commit 29f7fa7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
12 changes: 4 additions & 8 deletions src/async_impl/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@ impl Response {
/// ```
#[inline]
pub fn error_for_status(self) -> ::Result<Self> {
if self.status.is_client_error() {
Err(::error::client_error(*self.url, self.status))
} else if self.status.is_server_error() {
Err(::error::server_error(*self.url, self.status))
if self.status.is_client_error() || self.status.is_server_error() {
Err(::error::status_code(*self.url, self.status))
} else {
Ok(self)
}
Expand Down Expand Up @@ -202,10 +200,8 @@ impl Response {
/// ```
#[inline]
pub fn error_for_status_ref(&self) -> ::Result<&Self> {
if self.status.is_client_error() {
Err(::error::client_error(*self.url.clone(), self.status))
} else if self.status.is_server_error() {
Err(::error::server_error(*self.url.clone(), self.status))
if self.status.is_client_error() || self.status.is_server_error() {
Err(::error::status_code(*self.url.clone(), self.status))
} else {
Ok(self)
}
Expand Down
51 changes: 26 additions & 25 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ impl Error {
Kind::UrlBadScheme |
Kind::TooManyRedirects |
Kind::RedirectLoop |
Kind::ClientError(_) |
Kind::ServerError(_) |
Kind::Status(_) |
Kind::UnknownProxyScheme |
Kind::Timer => None,
}
Expand Down Expand Up @@ -206,7 +205,7 @@ impl Error {
#[inline]
pub fn is_client_error(&self) -> bool {
match self.inner.kind {
Kind::ClientError(_) => true,
Kind::Status(code) => code.is_client_error(),
_ => false,
}
}
Expand All @@ -215,7 +214,7 @@ impl Error {
#[inline]
pub fn is_server_error(&self) -> bool {
match self.inner.kind {
Kind::ServerError(_) => true,
Kind::Status(code) => code.is_server_error(),
_ => false,
}
}
Expand All @@ -224,8 +223,7 @@ impl Error {
#[inline]
pub fn status(&self) -> Option<StatusCode> {
match self.inner.kind {
Kind::ClientError(code) |
Kind::ServerError(code) => Some(code),
Kind::Status(code) => Some(code),
_ => None,
}
}
Expand Down Expand Up @@ -273,13 +271,15 @@ impl fmt::Display for Error {
Kind::Json(ref e) => fmt::Display::fmt(e, f),
Kind::TooManyRedirects => f.write_str("Too many redirects"),
Kind::RedirectLoop => f.write_str("Infinite redirect loop"),
Kind::ClientError(ref code) => {
f.write_str("Client Error: ")?;
fmt::Display::fmt(code, f)
}
Kind::ServerError(ref code) => {
f.write_str("Server Error: ")?;
fmt::Display::fmt(code, f)
Kind::Status(ref code) => {
let prefix = if code.is_client_error() {
"Client Error"
} else if code.is_server_error() {
"Server Error"
} else {
unreachable!("non-error status code: {:?}", code);
};
write!(f, "{}: {}", prefix, code)
}
Kind::UnknownProxyScheme => f.write_str("Unknown proxy scheme"),
Kind::Timer => f.write_str("timer unavailable"),
Expand Down Expand Up @@ -308,8 +308,15 @@ impl StdError for Error {
Kind::Json(ref e) => e.description(),
Kind::TooManyRedirects => "Too many redirects",
Kind::RedirectLoop => "Infinite redirect loop",
Kind::ClientError(_) => "Client Error",
Kind::ServerError(_) => "Server Error",
Kind::Status(code) => {
if code.is_client_error() {
"Client Error"
} else if code.is_server_error() {
"Server Error"
} else {
unreachable!("non-error status code: {:?}", code);
}
}
Kind::UnknownProxyScheme => "Unknown proxy scheme",
Kind::Timer => "timer unavailable",
}
Expand All @@ -335,8 +342,7 @@ impl StdError for Error {
Kind::UrlBadScheme |
Kind::TooManyRedirects |
Kind::RedirectLoop |
Kind::ClientError(_) |
Kind::ServerError(_) |
Kind::Status(_) |
Kind::UnknownProxyScheme |
Kind::Timer => None,
}
Expand All @@ -363,8 +369,7 @@ pub(crate) enum Kind {
Json(::serde_json::Error),
TooManyRedirects,
RedirectLoop,
ClientError(StatusCode),
ServerError(StatusCode),
Status(StatusCode),
UnknownProxyScheme,
Timer,
}
Expand Down Expand Up @@ -547,12 +552,8 @@ pub(crate) fn timedout(url: Option<Url>) -> Error {
Error::new(Kind::Io(io_timeout()), url)
}

pub(crate) fn client_error(url: Url, status: StatusCode) -> Error {
Error::new(Kind::ClientError(status), Some(url))
}

pub(crate) fn server_error(url: Url, status: StatusCode) -> Error {
Error::new(Kind::ServerError(status), Some(url))
pub(crate) fn status_code(url: Url, status: StatusCode) -> Error {
Error::new(Kind::Status(status), Some(url))
}

pub(crate) fn url_bad_scheme(url: Url) -> Error {
Expand Down

0 comments on commit 29f7fa7

Please sign in to comment.