iliana / rust-crowbar

Wrapper to simplify writing AWS Lambda functions in Rust (using the Python execution environment)
https://docs.rs/crowbar
Apache License 2.0
197 stars 16 forks source link

consider for rendering Display instead of Debug for errors reporting #42

Closed softprops closed 6 years ago

softprops commented 6 years ago

Rust's Error type error type requires both a Debug and Display implementation. One for developer debugging and one for human display. When a crowbar lambda fails it prints the errors Debug out which typically is not useful for humans but when lambda reports these errors to user Debug it what they say.

I ran some experiments and feel pretty confident that Display would be a much better way to go for the way the std::error::Error type and crates like failure represent errors for human consumption.

#[macro_use]
extern crate cpython;
#[macro_use]
extern crate crowbar;

use std::error;
use std::fmt;

#[derive(Debug)]
struct E(String);

/*impl fmt::Debug for E {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "this is a more manual debug")
    }
}*/

impl error::Error for E {
    fn description(&self) -> &str {
        "this is a description"
    }
}

impl fmt::Display for E {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "an error occured while trying perform a thing")
    }
}
lambda!(|event, _| {
    println!("invoked with {:?}", event);
    // this produces `SystemError: Rust panic`
    //panic!("nope");
    // this produces `RuntimeError: E("foo")`
    // but could produce `RuntimeError: an error occured while trying to perform a thing` if crowbar printed Display
    let result: Result<&str, Box<_>> = Err(E("foo".into()).into());
    result
});
softprops commented 6 years ago

Fixed by https://github.com/ilianaw/rust-crowbar/pull/43