# Pre-RFC: general error types

**URL:** <https://internals.rust-lang.org/t/pre-rfc-general-error-types/3358>\
**Category:** libs\
**Created:** [April 11, 2016, 5:52am UTC](https://internals.rust-lang.org/t/pre-rfc-general-error-types/3358 "2016-04-11T05:52:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FaultyRAM](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/faultyram/32/2645_2.png) [@FaultyRAM](https://internals.rust-lang.org/u/FaultyRAM)\
**Post date:** [April 11, 2016, 5:52am UTC](https://internals.rust-lang.org/t/pre-rfc-general-error-types/3358/1 "2016-04-11T05:52:14Z")

</div>

`Result<T, String>` is, generally speaking, bad. It’s lazy and it’s not idiomatic; if you want to fail with a descriptive error, you should instead use a type that implements `std::error::Error`. Unfortunately, there are times when it should be okay to be lazy. Maybe you want a descriptive error but don’t need to handle different kinds of errors, or you have a function that calls other functions that return a `Result`, but the error type is different in each case. For those situations (and probably others I haven’t thought of), you want a general-purpose error type, which libstd doesn’t currently have. Therefore, I’d like to propose the following additions to libstd:

- A new type, `std::error::WrappedError`, which implements `std::error::Error` such that calls to those methods are direct calls to an inner `Error` object:

```rust
use std::error::Error;

pub struct WrappedError { ... }

impl WrappedError {
    pub fn new<T>(error: T) -> WrappedError where T: Into<Box<Error>> { ... }
}

impl Error for WrappedError { ... }

```

- A new type, `std::error::GeneralError`, which holds a `String` and an `Option<Box<Error>>`, and implements `std::error::Error` such that `GeneralError::description()` returns the held string, and `GeneralError::cause()` returns an optional reference to the held error:

```rust
use std::error::Error;

pub struct GeneralError { ... }

impl GeneralError {
    pub fn new<T>(description: T) where T: Into<String> { ... }
    pub fn with_cause<T, C>(description: T, cause: C) where T: Into<String>, C: Into<Box<Error>> { ... }
}

```

What do you guys think?

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [April 11, 2016, 6:09am UTC](https://internals.rust-lang.org/t/pre-rfc-general-error-types/3358/2 "2016-04-11T06:09:14Z")

</div>

How is `WrappedError` any different to `Box<Error>`?

As for `GeneralError`, I’m not sure it’s _hugely_ useful. Really, all it’s adding is the ability to plaster a different message over the top of an existing error. Given how errors are typically rendered, this pretty much just _replaces_ the inner error with a string.

I’ve also tried to avoid doing this myself, because I worry about the perf hit of having each “layer” doing allocations to describe what went wrong when, really, it would be better to just define specialised enum/unitary types that should be a lot cheaper.

If you wanted to do this in general, wouldn’t it make more sense to have the “outer” error be any `Error` type, rather than just `String`? I mean, if nothing else, it should _at least_ be `Cow<'static, str>` to allow for fixed messages without allocation overhead.

Also, `GeneralError` is _supremely_ non-descriptive. `WrappedError`'s a better name for it.

**Edit** : Oh, and I think things like this are better written as external crates _first_, to prove they’re useful.

---

<div class="post-metadata">

**Author:** ![WiSaGaN](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wisagan/32/4027_2.png) [@WiSaGaN](https://internals.rust-lang.org/u/WiSaGaN)\
**Post date:** [April 11, 2016, 11:18am UTC](https://internals.rust-lang.org/t/pre-rfc-general-error-types/3358/3 "2016-04-11T11:18:13Z")

</div>

You can use [SimpleError](https://github.com/WiSaGaN/simple-error) for a change. Since you are not going to programically handle the error, I am not sure of the usefulness of having a cause. You can incorporate the cause error string directly into the simple error itself.

---

<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:** [March 25, 2019, 8:26am UTC](https://internals.rust-lang.org/t/pre-rfc-general-error-types/3358/4 "2019-03-25T08:26:03Z")

</div>

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