• chrash0@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 months ago

    i have thoughts.

    1. yes, custom errors are great. do them.

    2. don’t just wrap std::io::Error like that. same with request::Error. your custom errors are there to tell you about your domain. “there was an IO error” doesn’t mean a lot to your users. you could have wrapped that like AppError::ConfigLoadError or whatever; this is a big reason for having domain errors (or custom errors if you like) in the first place.

    3. thiserror is a great package with great docs that does a lot of this for you.

    4. don’t be afraid to nest your internal module errors. we use ConfigError all way down to something like OurBeskpokeFormatError, for example. then use impl From<ConfigError> for AppError. this of course depends on the scale of your application, but a 1000 line AppError enum is a mess.

    great advice generally! i’ve seen apps start off with anyhow and get stuck there with badly structured errors wedged in everywhere