# Result::inspect\_err, could be worth?

**URL:** <https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105>\
**Category:** libs\
**Created:** [February 23, 2021, 11:17am UTC](https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105 "2021-02-23T11:17:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dodomorandi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dodomorandi/32/4427_2.png) [@dodomorandi](https://internals.rust-lang.org/u/dodomorandi)\
**Post date:** [February 23, 2021, 11:17am UTC](https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105/1 "2021-02-23T11:17:47Z")

</div>

Something like this has been already asked [some time ago](https://internals.rust-lang.org/t/inspect-methods-for-result/6757), but almost no discussion came out.

The basic idea is handling a family of error for which you would be happy to `panic` in development phases, but they are pretty unexpected in production and `panic`ing would make things worse. The kind of situations in which you want to log the error and bubble it up.

To date, if you use chaining, you need to do something like this:

```rust
do_this()
    .and_then(/* .. */)
    .map(/* return a Result here */)
    .tranpose()
    .map_err(|err| {
        log!("Did not expect this! Err: {}. Data: {}", err, data);
        err
    })?
   .map(/* and so on */);

```

My point about using `map_err` instead of an hypothetical `inspect_err` is not about having to return `err`, but the fact that with `map_err` I am expressing that _I am going to change the error type of this `Result`_, which is not true.

However, I have seen people complaining about the _complexity_ of `Result` (and `Option`), and even if I don't agree1 I can understand that adding another method will _increase complexity_ even more. This can also be considered a _niche_ case, adding a _mental overhead_ for something not so useful (even if expressive).

Before opening a PR I would like to understand what you people think, if you ever needed this abstraction or not, and if you would consider adding the method something useful.

1 If you remove one of the many methods, you would need a `match` statement to express the same thing, making chaining impossible.

* * *

Practical real-life example: I build a `Stream` abstraction to get paged data from an Elasticsearch database. All the information is JSON encoded, therefore my approach is to analyze if more requests needs to be performed, eventually prepare the next one and trying to return the deserialized data. If a deserialization error occurs, I can log the original data and the error and bubble up the latter in order to make me understand the problem without causing a downtime. I took this specific example because it's easy to oversight a field that can be `null` in the DB but you did not encounter the case in months.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [February 23, 2021, 2:15pm UTC](https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105/2 "2021-02-23T14:15:18Z")

</div>

Just because you don’t specify its signature here and the original post also only describes it in words. This is about a function like this, right?

```rust
impl<T, E> Result<T, E> {
    pub fn inspect_err<F>(self, f: F) -> Self
    where
        F: FnOnce(&E),
    {
        self.map_err(|e| {f(&e); e})
    }
}

```

---

<div class="post-metadata">

**Author:** ![dodomorandi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dodomorandi/32/4427_2.png) [@dodomorandi](https://internals.rust-lang.org/u/dodomorandi)\
**Post date:** [February 23, 2021, 2:18pm UTC](https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105/3 "2021-02-23T14:18:52Z")

</div>

Yes, exactly! Sorry, I was focused on the idea and I totally forgot to write about the signature. You obviously got it right 😉

---

<div class="post-metadata">

**Author:** ![robinm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/robinm/32/6255_2.png) [@robinm](https://internals.rust-lang.org/u/robinm)\
**Post date:** [April 10, 2021, 8:58pm UTC](https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105/4 "2021-04-10T20:58:48Z")

</div>

I’m resurrecting this thread since I tried to use `my_option.inspect()` and realized that it doesn’t exist. Are `inspect` for `Option`/`Result` and `inspect_err` for Result things that we would like to add?

---

<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:** [July 9, 2021, 8:59pm UTC](https://internals.rust-lang.org/t/result-inspect-err-could-be-worth/14105/5 "2021-07-09T20:59:20Z")

</div>

Closed on July 9, 2021, 8:59pm UTC

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