Cleaner Debug representation of Error

This commit is contained in:
David Tolnay 2018-03-28 15:08:47 +02:00
parent 77a8c3af9c
commit 28ea01fbb9
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 15 additions and 4 deletions

View File

@ -179,7 +179,6 @@ impl From<Error> 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<str>),
@ -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
)
}
}

View File

@ -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::<Value>("{0}").unwrap_err();
let expected = "Error(\"key must be a string\", line: 1, column: 2)";
assert_eq!(format!("{:?}", err), expected);
}