Skip to content

Commit 525a618

Browse files
committed
Improve Debug format of error types
1 parent 5bb6c1d commit 525a618

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

src/error.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ use uri;
1313
/// functions in this crate, but all other errors can be converted to this
1414
/// error. Consumers of this crate can typically consume and work with this form
1515
/// of error for conversions with the `?` operator.
16-
#[derive(Debug)]
1716
pub struct Error {
1817
inner: ErrorKind,
1918
}
2019

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

24-
#[derive(Debug)]
2523
enum ErrorKind {
2624
StatusCode(status::InvalidStatusCode),
2725
Method(method::InvalidMethod),
@@ -34,9 +32,18 @@ enum ErrorKind {
3432
HeaderValueShared(header::InvalidHeaderValueBytes),
3533
}
3634

35+
impl fmt::Debug for Error {
36+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37+
f.debug_tuple("http::Error")
38+
// Skip the noise of the ErrorKind enum
39+
.field(&self.get_ref())
40+
.finish()
41+
}
42+
}
43+
3744
impl fmt::Display for Error {
3845
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39-
error::Error::description(self).fmt(f)
46+
fmt::Display::fmt(self.get_ref(), f)
4047
}
4148
}
4249

src/header/name.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ struct MaybeLower<'a> {
5656
}
5757

5858
/// A possible error when converting a `HeaderName` from another type.
59-
#[derive(Debug)]
6059
pub struct InvalidHeaderName {
6160
_priv: (),
6261
}
@@ -2015,6 +2014,14 @@ impl<'a> PartialEq<HeaderName> for &'a str {
20152014
}
20162015
}
20172016

2017+
impl fmt::Debug for InvalidHeaderName {
2018+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019+
f.debug_struct("InvalidHeaderName")
2020+
// skip _priv noise
2021+
.finish()
2022+
}
2023+
}
2024+
20182025
impl fmt::Display for InvalidHeaderName {
20192026
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20202027
self.description().fmt(f)

src/header/value.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub struct HeaderValue {
2525

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

591+
impl fmt::Debug for InvalidHeaderValue {
592+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
593+
f.debug_struct("InvalidHeaderValue")
594+
// skip _priv noise
595+
.finish()
596+
}
597+
}
598+
592599
impl fmt::Display for InvalidHeaderValue {
593600
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
594601
self.description().fmt(f)

src/method.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use std::str::FromStr;
4545
pub struct Method(Inner);
4646

4747
/// A possible error value when converting `Method` from bytes.
48-
#[derive(Debug)]
4948
pub struct InvalidMethod {
5049
_priv: (),
5150
}
@@ -390,6 +389,14 @@ impl InvalidMethod {
390389
}
391390
}
392391

392+
impl fmt::Debug for InvalidMethod {
393+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394+
f.debug_struct("InvalidMethod")
395+
// skip _priv noise
396+
.finish()
397+
}
398+
}
399+
393400
impl fmt::Display for InvalidMethod {
394401
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
395402
write!(f, "{}", self.description())

src/status.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub struct StatusCode(u16);
4646
///
4747
/// This error indicates that the supplied input was not a valid number, was less
4848
/// than 100, or was greater than 599.
49-
#[derive(Debug)]
5049
pub struct InvalidStatusCode {
5150
_priv: (),
5251
}
@@ -284,14 +283,6 @@ impl HttpTryFrom<u16> for StatusCode {
284283
}
285284
}
286285

287-
impl InvalidStatusCode {
288-
fn new() -> InvalidStatusCode {
289-
InvalidStatusCode {
290-
_priv: (),
291-
}
292-
}
293-
}
294-
295286
macro_rules! status_codes {
296287
(
297288
$(
@@ -512,6 +503,22 @@ status_codes! {
512503
(511, NETWORK_AUTHENTICATION_REQUIRED, "Network Authentication Required");
513504
}
514505

506+
impl InvalidStatusCode {
507+
fn new() -> InvalidStatusCode {
508+
InvalidStatusCode {
509+
_priv: (),
510+
}
511+
}
512+
}
513+
514+
impl fmt::Debug for InvalidStatusCode {
515+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
516+
f.debug_struct("InvalidStatusCode")
517+
// skip _priv noise
518+
.finish()
519+
}
520+
}
521+
515522
impl fmt::Display for InvalidStatusCode {
516523
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
517524
f.write_str(self.description())

0 commit comments

Comments
 (0)