In a small library that I’ve been working on, I have an errors module. While the code works properly, I’m unhappy with how it’s implemented; there’s a LOT of boilerplate code. I would like to reduce it, but I’m not entirely sure what’s the best way to do so. Here’s a link to the code.
The module is fairly simple: there are three
structs that encapsulate different errors that may happen within the library. These three
structs are then themselves encapsulated by an
ErrorKind is what’s returned by the library to the user, who then is expected to handle those errors.
My problem with this module is the amount of overhead involved when defining a new error. I have to define the
struct and it’s
impl, along with implementing the different traits that go along with
Error. Along with that, you have to implement the
From trait on
ErrorKind in order to create a new
ErrorKind from the new
struct. It’s annoying, and I don’t want to have to do it unless I have to. If I were to write this module in, say, Java, I’d simply create a base class that implements all of these different traits, which I would then extend when creating new errors objects.
Can anyone suggest an alternative way to implementing this module where there’s less overhead? I apologize for the crappy code. I know it’s clunky and terrible. Hence why I’m reaching out for help!