• jenesaisquoi@feddit.org
    link
    fedilink
    English
    arrow-up
    2
    ·
    10 hours ago

    For multiple types turning into one variant: you can either impl From for the others yourself, or you can have another enum with variants for them and have thiserror generate the From impl. I usually have lots of enums, at least one per module, sometimes even one per function, that form a tree. The errors you design should relate to the action that was being tried, and only encapsulate the lower error as context information.

    I see you’re using strings. You can do that, but then you’ll lose control over formatting, and additional context info that might be contained in them.

    Designing errors isn’t trivial and requires some experience. Try some things and over time you’ll see what works best.

    You can also check out https://docs.rs/snafu/latest/snafu/guide/index.html, another great library - their docs contain excellent guidance on designing error types.

    • treadful@lemmy.zip
      cake
      link
      fedilink
      English
      arrow-up
      1
      ·
      7 hours ago

      I see you’re using strings. You can do that, but then you’ll lose control over formatting, and additional context info that might be contained in them.

      I just haven’t figured out how to jam multiple error types into one enum variant yet. There’s no reason for the consumer of the module to get more pointless variants, so I’m not really sure I want an enum of enums, but maybe I’m overthinking it. I’ll fiddle more and check out your link later today.