Should the names of variants in a custom Error enum append the word Error
, or it sufficient to have the word Error
in the name of the enum itself?
For example, rather than MyError::IOError
, I would prefer MyError::IO
for the sake of brevity:
pub enum MyError {
IO,
ParseJSON,
// ...
}
I can think of two concerns.
-
If someone does a splat-import of all the enum variants via
use mylib::MyError::*
then a name likeIO
on its own will no longer connote "I'm an error type". But I think that's fine because rather than a splat-import I would ordinarily expectuse mylib
oruse mylib::MyError
; and if the library user really wants the bare enum variants they can do something likeuse mylib::MyError::IO as MyLibIOError
. -
The behavior of
#[derive(Debug)]
for an enum is to present the bare name of the enum variant, which is problematic a type likeMyError::IO
because it loses theMyError::
context. The solution is for the Error type to avoid#[derive(Debug)]
and instead provide an implementation ofDebug
which includes the name of the enum. (Playground)
Anything else I haven't thought of?
The Rust API Guidelines are mum on error naming conventions, only mentioning a pattern which is used incidentally by certain error types in std
but not consistently.