Skip to content

Commit

Permalink
Improve Debug format of error types
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Oct 7, 2019
1 parent 5bb6c1d commit 525a618
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
13 changes: 10 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ use uri;
/// functions in this crate, but all other errors can be converted to this
/// error. Consumers of this crate can typically consume and work with this form
/// of error for conversions with the `?` operator.
#[derive(Debug)]
pub struct Error {
inner: ErrorKind,
}

/// A `Result` typedef to use with the `http::Error` type
pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
enum ErrorKind {
StatusCode(status::InvalidStatusCode),
Method(method::InvalidMethod),
Expand All @@ -34,9 +32,18 @@ enum ErrorKind {
HeaderValueShared(header::InvalidHeaderValueBytes),
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("http::Error")
// Skip the noise of the ErrorKind enum
.field(&self.get_ref())
.finish()
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
error::Error::description(self).fmt(f)
fmt::Display::fmt(self.get_ref(), f)
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/header/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ struct MaybeLower<'a> {
}

/// A possible error when converting a `HeaderName` from another type.
#[derive(Debug)]
pub struct InvalidHeaderName {
_priv: (),
}
Expand Down Expand Up @@ -2015,6 +2014,14 @@ impl<'a> PartialEq<HeaderName> for &'a str {
}
}

impl fmt::Debug for InvalidHeaderName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("InvalidHeaderName")
// skip _priv noise
.finish()
}
}

impl fmt::Display for InvalidHeaderName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
Expand Down
9 changes: 8 additions & 1 deletion src/header/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct HeaderValue {

/// A possible error when converting a `HeaderValue` from a string or byte
/// slice.
#[derive(Debug)]
pub struct InvalidHeaderValue {
_priv: (),
}
Expand Down Expand Up @@ -589,6 +588,14 @@ fn is_valid(b: u8) -> bool {
b >= 32 && b != 127 || b == b'\t'
}

impl fmt::Debug for InvalidHeaderValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("InvalidHeaderValue")
// skip _priv noise
.finish()
}
}

impl fmt::Display for InvalidHeaderValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
Expand Down
9 changes: 8 additions & 1 deletion src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ use std::str::FromStr;
pub struct Method(Inner);

/// A possible error value when converting `Method` from bytes.
#[derive(Debug)]
pub struct InvalidMethod {
_priv: (),
}
Expand Down Expand Up @@ -390,6 +389,14 @@ impl InvalidMethod {
}
}

impl fmt::Debug for InvalidMethod {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("InvalidMethod")
// skip _priv noise
.finish()
}
}

impl fmt::Display for InvalidMethod {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
Expand Down
25 changes: 16 additions & 9 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub struct StatusCode(u16);
///
/// This error indicates that the supplied input was not a valid number, was less
/// than 100, or was greater than 599.
#[derive(Debug)]
pub struct InvalidStatusCode {
_priv: (),
}
Expand Down Expand Up @@ -284,14 +283,6 @@ impl HttpTryFrom<u16> for StatusCode {
}
}

impl InvalidStatusCode {
fn new() -> InvalidStatusCode {
InvalidStatusCode {
_priv: (),
}
}
}

macro_rules! status_codes {
(
$(
Expand Down Expand Up @@ -512,6 +503,22 @@ status_codes! {
(511, NETWORK_AUTHENTICATION_REQUIRED, "Network Authentication Required");
}

impl InvalidStatusCode {
fn new() -> InvalidStatusCode {
InvalidStatusCode {
_priv: (),
}
}
}

impl fmt::Debug for InvalidStatusCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("InvalidStatusCode")
// skip _priv noise
.finish()
}
}

impl fmt::Display for InvalidStatusCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description())
Expand Down

0 comments on commit 525a618

Please sign in to comment.