refactor(error): improve error handling#252
Conversation
|
Something seems wonky with CI. afaict, checks are only running when the PR is first created, and not for subsequent push(es). or at least it does not run for some subsequent pushes. is this intentional? |
f6e05cf to
2456a70
Compare
|
ok, so I was getting some emails from github that CI was failing. Here's an example: It was some kind of cargo bench issue. I found a Cargo.toml workaround, and pushed changes for it to this branch. After which, I get another kind of failure: maybe it wants me to have a gh-pages branch for some reason? |
Addresses issue kindelia#247. (more to come later) kindelia_core: * removes all unwrap() from net.rs * defines error enums in net.rs: ProtoCommError and ParseAddressError * return Result from affected fn in net.rs * change IPV6 panic!() to unimplemented!() * accept addr arg for Node::new() so it can avoid returning Result * update test and bench as necessary kindelia: * add dep on anyhow crate * return anyhow::Result from config methods * return anyhow::Result from main methods, including main() * return anyhow::Result from FileInput::read_to_string()
Fixes calls to anyhow!() macro of these two forms:
* anyhow!(format(x,y)) --> anyhow!(x,y)
* anyhow!("...".to_string()) --> anyhow!("...")
I think this is solved in ab32905, but you can talk if you have any problems.
This seems very good.
I think this is better, since this is smaller. |
Addresses kindelia#247. use anyhow::Context trait to Replace calls to map_err(|e| anyhow!("...{}", e)) with .context("..."). This does two things: 1. Makes the code and error messages more succinct and readable. 2. Preserves the original error
|
I just force pushed with these changes:
|
Addresses issue #247. (part a: more to come later)
I started small by just fixing 4 unwrap() calls in kindelia_core/src/net.rs.
Unfortunately one of these involved a trait, which complicated things a bit and required small changes in other files.
In kindelia
main.rsI could have just mapped the errors from net.rs into a string and submitted a smaller PR, but instead I decided it would be cleaner to handle them asanyhow::Result.I ended up changing most fn in
main.rsandconfig.rsto returnanyhow::Resultinstead ofResult<_, String>. This required some calls tomap_err(|e| anyhow!(e)to deal withStringerrors fromformat!()or from lower level fn calls. However, as the lower level calls get fixed, then themap_errgoes away and we will just havefn_call()?. very clean.note: the map_err() is necessary because
anyhow::Resultacceptsanyhow::Errorwhich is a boxed dynamic error type that accepts any error type derived fromstd::error::Error. HoweverStringis not so derived and thus must be mapped (or preferably returnanyhow::Resultor a thiserror type instead).So this is very much a preliminary / intermediate PR to introduce anyhow and show the direction.
I understand there is a big refactor happening in another branch. I am ok with rebasing onto it when merged. Or this could be merged first, whichever works.
kindelia_core:
kindelia: