pyerror: stringify Rust errors with "{:?}"

Summary:
According to the anyhow documentation[0], the behavior of `.to_string()` is to
only stringify the top-level errors, hiding all the context of the error.
Instead, the debug format allows all the context to be displayed, and, if
available the backtrace.

This should significantly help debug Rust errors when context is available,
which we should strive to have everywhere!

[0]: https://docs.rs/anyhow/1.0.27/anyhow/struct.Error.html#display-representations

Reviewed By: sfilipco

Differential Revision: D20575944

fbshipit-source-id: 2968d7fb755edec7f7e5151138e8049ded181c1b
This commit is contained in:
Xavier Deguillard 2020-03-20 20:19:55 -07:00 committed by Facebook GitHub Bot
parent 64bec39f5c
commit 767134797c
2 changed files with 6 additions and 6 deletions

View File

@ -38,22 +38,22 @@ fn register_error_handlers() {
if let Some(e) = e.downcast_ref::<indexedlog::Error>() {
Some(PyErr::new::<IndexedLogError, _>(
py,
cpython_ext::Str::from(e.to_string()),
cpython_ext::Str::from(format!("{:?}", e)),
))
} else if let Some(e) = e.downcast_ref::<metalog::Error>() {
Some(PyErr::new::<MetaLogError, _>(
py,
cpython_ext::Str::from(e.to_string()),
cpython_ext::Str::from(format!("{:?}", e)),
))
} else if let Some(e) = e.downcast_ref::<revisionstore::Error>() {
Some(PyErr::new::<RevisionstoreError, _>(
py,
cpython_ext::Str::from(e.to_string()),
cpython_ext::Str::from(format!("{:?}", e)),
))
} else if let Some(e) = e.downcast_ref::<cpython_ext::Error>() {
Some(PyErr::new::<NonUTF8Path, _>(
py,
cpython_ext::Str::from(e.to_string()),
cpython_ext::Str::from(format!("{:?}", e)),
))
} else {
None
@ -61,7 +61,7 @@ fn register_error_handlers() {
}
fn fallback_error_handler(py: Python, e: &error::Error) -> Option<PyErr> {
Some(PyErr::new::<RustError, _>(py, e.to_string()))
Some(PyErr::new::<RustError, _>(py, format!("{:?}", e)))
}
error::register("010-specific", specific_error_handler);

View File

@ -113,7 +113,7 @@ impl<T, E: Into<Error>> ResultPyErrExt<T> for Result<T, E> {
}
// Nothing matches. Fallback to RuntimeError.
// Hopefully this is not really used.
return cpython::PyErr::new::<exc::RuntimeError, _>(py, e.to_string());
return cpython::PyErr::new::<exc::RuntimeError, _>(py, format!("{:?}", e));
}
})
}