about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-06-21 03:08:28 +0000
committerbors <bors@rust-lang.org>2015-06-21 03:08:28 +0000
commitecfcd2a305dfe360f4fbf1776a49d7fc04de3322 (patch)
tree5737232235c6400e72d4e1f43e9159c3ada79af2
parent7d6e79001083140a5697565b498d67c90e0a5ff7 (diff)
parentc8aec53db73182f453621c284131ef028d0ae064 (diff)
downloadrust-ecfcd2a305dfe360f4fbf1776a49d7fc04de3322.tar.gz
rust-ecfcd2a305dfe360f4fbf1776a49d7fc04de3322.zip
Auto merge of #26416 - retep998:error-debug, r=sfackler
Fixes https://github.com/rust-lang/rust/issues/26408

Example output when using `Result::unwrap`:
```
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 2, message: "The system cannot find the file specified.\r\n" } }', src/libcore\result.rs:731
```

This could technically be considered a breaking change for any code that depends on the format of the `Debug` output for `io::Error` but I sincerely hope nobody wrote code like that.
-rw-r--r--src/libstd/io/error.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/libstd/io/error.rs b/src/libstd/io/error.rs
index b43ac0daf51..a66789bf287 100644
--- a/src/libstd/io/error.rs
+++ b/src/libstd/io/error.rs
@@ -37,7 +37,6 @@ pub struct Error {
     repr: Repr,
 }
 
-#[derive(Debug)]
 enum Repr {
     Os(i32),
     Custom(Box<Custom>),
@@ -240,6 +239,17 @@ impl Error {
     }
 }
 
+impl fmt::Debug for Repr {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            &Repr::Os(ref code) =>
+                fmt.debug_struct("Os").field("code", code)
+                   .field("message", &sys::os::error_string(*code)).finish(),
+            &Repr::Custom(ref c) => fmt.debug_tuple("Custom").field(c).finish(),
+        }
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl fmt::Display for Error {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
@@ -282,6 +292,16 @@ mod test {
     use error;
     use error::Error as error_Error;
     use fmt;
+    use sys::os::error_string;
+
+    #[test]
+    fn test_debug_error() {
+        let code = 6;
+        let msg = error_string(code);
+        let err = Error { repr: super::Repr::Os(code) };
+        let expected = format!("Error {{ repr: Os {{ code: {:?}, message: {:?} }} }}", code, msg);
+        assert_eq!(format!("{:?}", err), expected);
+    }
 
     #[test]
     fn test_downcasting() {