
On Mon, Jan 21, 2019 at 6:14 PM Sorin Fetche via Boost < boost@lists.boost.org> wrote:
On Wed, Jan 16, 2019 at 11:51 AM Emil Dotchevski via Boost <boost@lists.boost.org> wrote:
LEAF is a C++11 error handling library for use in low-latency
environments.
- Can be used with or without exception handling.
Is it possible to have a common error handling logic for both error codes and exceptions? Something like:
leaf::result<T> function_that_may_throw(); ... leaf::handle_all( [i]() -> leaf::result<int> { LEAF_AUTO(r, leaf::exception_to_result<std::exception>([] { function_that_may_throw(); })); ... return 0; }, [](std::exception const &e) { // Handle exception(s) return 1; }, [](custom_error_code ec) { // Handle error code(s) return 2; })
The attached program shows that once leaf::exception_to_result() is used, only the exceptions reach the handlers in `handle_all`.
You found a bug, thank you. I had not tested exception_to_result with a function that returns result<T>, and it incorrectly deduced its return type as result<result<T>>. I've fixed this in my current working branch, "feature/error_code", feel free to use that for testing. This new refactoring pass will make it to "master" in a day probably. However you're raising an interesting question, should I add explicit support for handling errors from functions which return result<T> but may also throw? I don't like exception_to_result very much, I think it can be useful but the slicing feels a little dangerous. I'll make leaf::try_ detect when the passed try_block returns a result<T>, and handle errors like leaf::handle_some, in addition to catching exceptions. Thoughts?