# Should NoneError implement the Error trait?

**URL:** <https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312>\
**Category:** libs\
**Created:** [November 28, 2017, 6:00pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312 "2017-11-28T18:00:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![smarnach](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/smarnach/32/2998_2.png) [@smarnach](https://internals.rust-lang.org/u/smarnach)\
**Post date:** [November 28, 2017, 6:00pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/1 "2017-11-28T18:00:19Z")

</div>

I just experimented with using the `?` operator with `Option<T>` and was surprised to find that the resulting error wouldn’t convert into `Box<Error>`. It’s not that I have a real use case for this – instead of propagating a rather meaningless `NoneError`, it would certainly be better to implement a conversion into a custom error type. I was surprised anyway, since currently `NoneError` is the only error type in the standard that does not implement the `Error` trait. Is this difference intentional?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [November 29, 2017, 1:34am UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/2 "2017-11-29T01:34:03Z")

</div>

I don’t recall it being discussed in the RFC or the PR.

Personally, it not existing so people use `.ok_or(something)?` in a `Result` method feels reasonable.

---

<div class="post-metadata">

**Author:** ![thedodd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/thedodd/32/2339_2.png) [@thedodd](https://internals.rust-lang.org/u/thedodd)\
**Post date:** [December 8, 2017, 7:01pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/3 "2017-12-08T19:01:25Z")

</div>

I certainly agree with @smarnach on this. It is a bit unclear to me as to why it does not implement `Error`. It is at least a bit deceiving that it is called `NoneError` but does not implement `Error`.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [December 8, 2017, 8:16pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/4 "2017-12-08T20:16:37Z")

</div>

Either way it's not great:

- Lack of the `Error` implementation looks like an inconsistency, and when used with `Box<Error>` gives an IMHO unfriendly error message:

- `NoneError` does not contain file/line number that caused it, so when wrapped in `Box<Error>` it may become coompletely detached from the place that generated the error, and may be super hard to figure out what caused it in a large application.

So I'd suggest:

1. Make `NoneError` compiled in debug mode display (via `fmt::Debug`?) the file/line it came from.

2. If it's not supposed to implement `Error`, customize E0277 to suggest using `.ok_or("msg")`

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [December 8, 2017, 11:10pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/5 "2017-12-08T23:10:49Z")

</div>

[I rather got the impression that the omission is deliberate](https://github.com/rust-lang/rust/pull/42526#issuecomment-309912689)

---

<div class="post-metadata">

**Author:** ![lacourc](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lacourc](https://internals.rust-lang.org/u/lacourc)\
**Post date:** [December 10, 2017, 2:37am UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/6 "2017-12-10T02:37:00Z")

</div>

> [@kornel](#):
>
> customize E0277 to suggest using .ok\_or("msg")

👍

---

<div class="post-metadata">

**Author:** ![U007D](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/u007d/32/2172_2.png) [@U007D](https://internals.rust-lang.org/u/U007D)\
**Post date:** [March 16, 2018, 1:57pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/7 "2018-03-16T13:57:26Z")

</div>

Hi, everyone.

I have seen a lot of activity around this question, but have not been able to find a clear, succinct explanation as to why `NoneError` should not implement the `Error` trait.

A few examples:

- “I’m not sure myself”: [https://github.com/rust-lang/rust/pull/42526#issuecomment-311065346](https://github.com/rust-lang/rust/pull/42526#issuecomment-311065346)
- “I rather got the impression that the omission is deliberate”: [Should NoneError implement the Error trait?](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/5?u=u_007d)
- Q asked but not answered: [https://github.com/rust-lang/rust/pull/42526#pullrequestreview-57652383](https://github.com/rust-lang/rust/pull/42526#pullrequestreview-57652383)
- conversations with multiple people on #rust-beginners IRC came to the same conclusions (no clear understanding of the rationale)

The actual reasoning is very unclear (at least to me). Could some kind soul please summarize (or point to a succinct summary of) the reasoning?

Motivation:

- “something I really want”: [https://github.com/rust-lang-nursery/failure/issues/59#issuecomment-347512030](https://github.com/rust-lang-nursery/failure/issues/59#issuecomment-347512030)
- “I scoured the docs and had a bit of a hard time”: [https://github.com/rust-lang-nursery/failure/issues/61](https://github.com/rust-lang-nursery/failure/issues/61)

Personally, I must use `.ok_or(MyError::MyVariant)?` instead of just `?` when I need to convert a `None` into an `Result::Err`, and it chafes every single time. Either implementing the `Error` trait for `NoneError` or having a better understanding of why the extra boilerplate should be necessary would be very much appreciated.

-U007D

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [March 16, 2018, 4:07pm UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/8 "2018-03-16T16:07:10Z")

</div>

Personally, I still feel unclear on whether `NoneError` should exist at all. I’ve not been especially happy with the setup where the `Try` trait makes all `?` types be isomorphic with result, [as I wrote about here](https://github.com/rust-lang/rust/issues/42327#issuecomment-366840247) – it seems to diminish the applicability of `Try` significantly to me. The need to define an artificial `NoneError` to make `Option` fit (or else use `()`, which also feels somewhat artificial to me, but would be more “common”) is sort of a case in point.

---

<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:29am UTC](https://internals.rust-lang.org/t/should-noneerror-implement-the-error-trait/6312/9 "2019-03-25T08:29:19Z")

</div>

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