Application specific errors

I have a library which uses application-supplied closures. Those closures need to be able to return Result. There are a few predefined errors, but the application may want/need to supply application specific errors. I can choose to implement this statically or dynamically:

enum Error<E> {
  InvalidInput(String),
  // ...
  App(E)
}

vs

enum Error {
  InvalidInput(String),
  // ...
  App(Box<dyn std::error::Error>)
}

For this library:

  • an application will likely "never" have more than one instance of Error, so it's very unlikely that it'll instantiate more than one of Error<E>.
  • the snippets are trimmed down; most of the library is generic over another application-defined type, using the dynamic version solely to avoid instantiation of generics won't actually accomplish much.

I am leaning towards the dynamic solution, but I'm curious what people prefer as users of a library, and why.

Are there any other commonly used "application specific errors" patterns that I should be considering?

The closure may return Result<T, E> where E: From<MyError> where MyError is an enum containing those predefined errors.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.