From 28ea01fbb9cd320b2fff9d4e4688d8c10d6d55e5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 28 Mar 2018 15:08:47 +0200 Subject: [PATCH] Cleaner Debug representation of Error --- src/error.rs | 10 +++++++--- tests/debug.rs | 9 ++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 57b7da7..4ef9048 100644 --- a/src/error.rs +++ b/src/error.rs @@ -179,7 +179,6 @@ impl From for io::Error { } } -#[derive(Debug)] struct ErrorImpl { code: ErrorCode, line: usize, @@ -188,7 +187,6 @@ struct ErrorImpl { // Not public API. Should be pub(crate). #[doc(hidden)] -#[derive(Debug)] pub enum ErrorCode { /// Catchall for syntax error messages Message(Box), @@ -382,7 +380,13 @@ impl Display for ErrorImpl { // end up seeing this representation because it is what unwrap() shows. impl Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - Debug::fmt(&*self.err, f) + write!( + f, + "Error({:?}, line: {}, column: {})", + self.err.code.to_string(), + self.err.line, + self.err.column + ) } } diff --git a/tests/debug.rs b/tests/debug.rs index c4411f3..f88828c 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate serde_json; -use serde_json::Number; +use serde_json::{Number, Value}; #[test] fn number() { @@ -45,3 +45,10 @@ fn value_array() { fn value_object() { assert_eq!(format!("{:?}", json!({})), "Object({})"); } + +#[test] +fn error() { + let err = serde_json::from_str::("{0}").unwrap_err(); + let expected = "Error(\"key must be a string\", line: 1, column: 2)"; + assert_eq!(format!("{:?}", err), expected); +}