# Simplify error handling

**URL:** <https://internals.rust-lang.org/t/simplify-error-handling/12226>\
**Category:** language design\
**Created:** [April 24, 2020, 4:12pm UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226 "2020-04-24T16:12:00Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![redradist](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/redradist/32/6475_2.png) [@redradist](https://internals.rust-lang.org/u/redradist)\
**Post date:** [April 24, 2020, 10:32pm UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226/21 "2020-04-24T22:32:39Z")

</div>

> [@josh](#):
>
> Depending on what you're looking for, you could also use `anyhow::Result<u32>`, which will wrap any error you like and not require creating any types. You can still get a specific error type out of it if you need to (using downcasting), and in the common case where all errors just get reported without being matched on, you don't need to do anything at all.
> 
> Might that give you the reduced boilerplate you're looking for?

It is nice !!

But I think it would be better anyway to use anonymous enums, because it do not enforce user type to impl `From` trait and much better match expression

Also anonymous enums is usable for other types as &[Error | MyError] and so on ...

---

<div class="post-metadata">

**Author:** ![redradist](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/redradist/32/6475_2.png) [@redradist](https://internals.rust-lang.org/u/redradist)\
**Post date:** [May 16, 2020, 8:43pm UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226/22 "2020-05-16T20:43:23Z")

</div>

@RustyYato @josh @pythoneer @CAD97 @Aloso @Ixrec

I have created crate [https://crates.io/crates/ferris-extensions](https://crates.io/crates/ferris-extensions) where is implemented macro that generate anonymous enum:

```rust
#[multiple_result_errors]
fn handle_file() -> Result<(), (IOError, IOError2)>
{
    get_io_error()?;
    Ok(())
}
fn main() {
    let res = handle_file();
    match res {
        Ok(t) => {},
        Err(err) => {
            match err {
                HandleFileResultErrors::IOError(err0) => {},
                HandleFileResultErrors::IOError2(err1) => {},
            };
        }
    };
}

```

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [May 16, 2020, 11:06pm UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226/23 "2020-05-16T23:06:29Z")

</div>

Some documentation would be great! Also, why is there [another macro](https://docs.rs/ferris-extensions/0.1.0/ferris_extensions/attr.boxed_async_recursion.html) in the crate?

---

<div class="post-metadata">

**Author:** ![tkaitchuck](https://avatars.discourse-cdn.com/v4/letter/t/eb8c5e/32.png) [@tkaitchuck](https://internals.rust-lang.org/u/tkaitchuck)\
**Post date:** [May 17, 2020, 6:18am UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226/24 "2020-05-17T06:18:13Z")

</div>

A major problem in implementing error handling is that the implementation must choose between a `Box<dyn Error>>` or a `Result` with an `enum` error. There is no good way to abstract over this distinction.

If there were an enum `MaybeBox<T>` that holds a `T` when `T` is `Sized` and holds a `Box<T>` when `T` is unsized. It would be possible to have a type like `Result` which can work with either approach. As far as I can tell this cannot be expressed within the type system.

Is there any way around this?

---

<div class="post-metadata">

**Author:** ![redradist](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/redradist/32/6475_2.png) [@redradist](https://internals.rust-lang.org/u/redradist)\
**Post date:** [May 17, 2020, 8:04am UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226/25 "2020-05-17T08:04:40Z")

</div>

> [@tkaitchuck](#):
>
> A major problem in implementing error handling is that the implementation must choose between a `Box<dyn Error>>` or a `Result` with an `enum` error. There is no good way to abstract over this distinction.
> 
> If there were an enum `MaybeBox<T>` that holds a `T` when `T` is `Sized` and holds a `Box<T>` when `T` is unsized. It would be possible to have a type like `Result` which can work with either approach. As far as I can tell this cannot be expressed within the type system.
> 
> Is there any way around this?

Some times it is not possible to use `Box<dyn Error>>` or not desired (for example Embedded System) ... Sometime better to have anonymous enum in Result of error that can return either IOError, IOError2, KeyboardError and so on

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 16, 2020, 12:54am UTC](https://internals.rust-lang.org/t/simplify-error-handling/12226/27 "2020-08-16T00:54:55Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/simplify-error-handling/12226.md?page=1)
