Stream: project-error-handling

Topic: remedial education for Niko


view this post on Zulip nikomatsakis (Jun 23 2021 at 18:18):

Hey @Jane Lusby or others. What is the easiest and correct way to attach context to errors these days? I am using anyhow and I basically just want to know (e.g.) which file it is that I can't open anyway

view this post on Zulip nikomatsakis (Jun 23 2021 at 18:18):

just putting ? isn't getting me the backtrace I crave :)

view this post on Zulip Sean McArthur (Jun 23 2021 at 18:21):

Perhaps with the anyhow::Context trait? let buf = fs::read(path).with_context(|| format!("failed to read {:?}", path.display()))?

view this post on Zulip Jane Lusby (Jun 23 2021 at 18:21):

I think it depends on the situation. Given that you already have anyhow and you're presumably not worrying about how this affects how you can react to specific error cases then using .context provided by anyhow would probably be easiest

view this post on Zulip Jane Lusby (Jun 23 2021 at 18:21):

as Sean said

view this post on Zulip Josh Triplett (Jun 23 2021 at 18:22):

That's what I'd suggest as well.

view this post on Zulip Jane Lusby (Jun 23 2021 at 18:22):

alternatively if you're using tracing and you only need this additional context for debugging and would prefer to not have it shown in error reports to users I would recommend using tracing-error and eyre

view this post on Zulip Jane Lusby (Jun 23 2021 at 18:22):

since that's exactly what they were designed for

view this post on Zulip Josh Triplett (Jun 23 2021 at 18:22):

There are also some helper libraries that give you functions with the same signature as fs::read or fs::open that automatically attach error context.

view this post on Zulip Josh Triplett (Jun 23 2021 at 18:22):

Depends on where you're using it and if you mind the additional overhead of doing it for every operation.

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:09):

I don't care at all about overhead

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:09):

but I care about the ergonomic hit of .with_context(| format!(...)), which feels awfully clunky

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:09):

maybe good enough I guess

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:10):

postfix macross

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:10):

@Jane Lusby do I want eyre? I care about how pretty things look when they print out

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:10):

and I care about not having to work very hard

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:10):

nikomatsakis said:

Jane Lusby do I want eyre? I care about how pretty things look when they print out

if you want to control the format then you definitely want eyre

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:10):

this is for https://github.com/optopodi/optopodi/

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:10):

unless you're happy with anyhow's defaults

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:10):

because anyhow isn't configurable

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:10):

probably I don't care that much now, but I might eventually care

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:11):

does eyre support "everything gets upcast to a single easy type"?

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:11):

if you want it to be easy and nice looking https://docs.rs/color-eyre/0.5.11/color_eyre/ is supposed to be a reasonable defaults for a fancy reporting hook

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:11):

nikomatsakis said:

does eyre support "everything gets upcast to a single easy type"?

not sure what you mean

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:11):

anyhow::Error is very convenient

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:11):

it supports pretty much everything anyhow does

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:11):

its a fork of anyhow

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:11):

got it

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:11):

it even re-exports a lot of the renamed types with the original names

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:11):

so it should be pretty easy to drop in swap

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:12):

ok

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:12):

I'll do that at some point then

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:12):

sick :D

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:12):

I would like it if there were a way to get backtraces by default with zero effort on my part

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:12):

(and then I can later add nice, user-understable context)

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:12):

is that a thing?

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:12):

stable-eyre is the "just give me backtraces and nothing else" hook i made

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:13):

https://docs.rs/stable-eyre/0.2.2/stable_eyre/

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:13):

ack

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:13):

otherwise same format as default

view this post on Zulip nikomatsakis (Jun 25 2021 at 18:13):

that sounds useful

view this post on Zulip Jane Lusby (Jun 25 2021 at 18:13):

uses backtrace-rs so it works on stable, but you can't extract it through the Error trait for obvious reasons


Last updated: Jan 29 2022 at 11:01 UTC