Stream: project-error-handling

Topic: "error codes"


view this post on Zulip Charles Lew (Sep 21 2020 at 16:42):

When passing errors to existing outer systems, many times we need unique identifiers for them to recognize or classify the error type. For many systems, these are just integers as error codes. For example, 40x status code in a http rest API.

I wonder if there's some existing prior arts on this part.

view this post on Zulip Jake Goulding (Sep 21 2020 at 16:46):

What exactly would you want to see?

Errors are types and types can have methods that return values or associated constants. Types can have traits implemented for them that have methods or associated constants.

view this post on Zulip Jake Goulding (Sep 21 2020 at 16:52):

There's https://github.com/rust-lang/rust/issues/60553 which will allow this:

#![feature(arbitrary_enum_discriminant)]

#[repr(u8)]
#[derive(Copy, Clone)]
enum Code {
    Alpha,
}

#[repr(u8)]
enum Error {
    Alpha { id: i32 } = Code::Alpha as u8,
}

impl Error {
    fn code(&self) -> Code {
        unsafe { *(self as *const Self as *const Code) }
    }
}

fn main() {
    let code = Error::Alpha { id: 99 }.code();
}

view this post on Zulip Charles Lew (Sep 21 2020 at 17:02):

not entirely sure, maybe something like

enum Error {
     #[error_code(100)]
     ErrorA,
     #[error_code(101)]
     ErrorB,
}

Anything that can help me avoid repeating myself is good in this case

view this post on Zulip Jake Goulding (Sep 21 2020 at 17:11):

What kind of repetition are you worried about?

view this post on Zulip Jake Goulding (Sep 21 2020 at 17:14):

One place you can look at is rustc's own error codes.

view this post on Zulip Charles Lew (Sep 21 2020 at 17:27):

I meant repetitions like listing all the variants at the definition site and match and list them again and again in some fictional impl methods.

But yes, in this case RFC 2363 might be just enough, since it's quite natural to let the discriminant correspond to the actual error codes. Just needs to wait for its stablization here. :sweat_smile:

view this post on Zulip Jake Goulding (Sep 21 2020 at 17:28):

Charles Lew said:

all the variants at the definition site and match and list them again and again in some fictional impl methods.

Why would you list them in both places? Just the one match would be enough.

view this post on Zulip Jake Goulding (Sep 21 2020 at 17:30):

I think the harder part is around never reusing an error code, never duplicating it, or spreading the codes across multiple types.

view this post on Zulip Markus Unterwaditzer (Sep 21 2020 at 20:50):

I think you might want to check out strum, they have a derive for attaching attributes to enum variants

view this post on Zulip Markus Unterwaditzer (Sep 21 2020 at 20:50):

https://github.com/Peternator7/strum/wiki/Derive-EnumProperty

view this post on Zulip Yoshua Wuyts (Sep 21 2020 at 21:56):

For http_types::Error we've essentially combined a StatusCode enum + an anyhow::Error https://docs.rs/http-types/2.4.0/http_types/struct.Error.html — though I'm not loving this pattern, it seems to work okay


Last updated: Jan 26 2022 at 14:20 UTC