rust-lang-deprecated / error-chain

Error boilerplate for Rust
Apache License 2.0
729 stars 111 forks source link

log feature - support use of the log crate #239

Open rusterize opened 6 years ago

rusterize commented 6 years ago

Hi,

This pull request allows the use of the log crate with error chain. It extends Result and Error with functions to log the errors and their causes. Please let me know if you would be willing to accept it. Example:

...
let mut req = cl.get(&*url)
        .send()
        .chain_err(|| GetUrl(url.to_string(), "Failed to read".to_string()))
        .loge()?;
...

This will print the following given a badurl:

ERROR|rget::errors|Unable open url: 'badurl' : 'Failed to read'
ERROR|rget::errors|     caused by: relative URL without a base

Features:

Missing:

rusterize commented 6 years ago

NOTE: travis test with rust 1.10 fails because 1.10 does not support #[cfg(feature(...)] for conditional macro execution. I don't think there is any need to spend time on this, since 1.11 works OK. If you agree, maybe the travis minimum version should be updated to rust 1.11

est31 commented 6 years ago

Can't this be put into a separate crate? This would be the better solution IMO as the feature is mostly orthogonal.

rusterize commented 6 years ago

@est31 It can't be put in a separate crate, because the log.. functions are intended to log both the acual error and the chain of errors (the causes). If we did it in a separate crate there would be no access to the internal error object that contains all the causes, which is the whole point of logging right at the point of failure (to see the error and its causes).

BTW, one of the reasons for the new failure crate is precisely to solve this problem, and to allow downcasting the error to the internal errors whilst providing access to the causes.