Ability to check if the error belongs to dig
Is your feature request related to a problem? Please describe. During our application initialization I want to distinguish errors that my constructors throw from DI graph issues. I need to know this to provide different explanations to the developer. Also, I don't want to show the whole DI graph when the error is trivial (e.g. env var is not set).
Describe the solution you'd like
I would like to see a function like dig.DigError(err) or dig.CausedByConstructor(err) (ugly)
Describe alternatives you've considered
An API like go errors would also work here: dig.IsError, dig.AsError etc. It would be tricky to test against a number of dig errors though, so this seems suboptimal to me
Is this a breaking change? No, it's a new public method
@AnatolyRugalev thanks for the request. I think some way of distinguishing dig errors makes sense.
Meanwhile, is dig.RootCause an option? It won't report whether something is a dig error, but it will unpack to the first non-dig error.
Yeah, I am using RootCause and some secondary assertions to get the job done:
cause := dig.RootCause(err)
myErr := new(myerror.MyError)
isMyErr := errors.As(cause, &myError)
_, isFormatterErr := cause.(fmt.Formatter)
if !isMyErr && isFormatterErr {
fmt.Printf("fatal DI error: %+v\n", err)
os.Exit(3)
}
fmt.Printf("error initializing server: %+v\n", cause)
os.Exit(2)
As you can see, I can check whether an error is coming from a known type, but not the opposite, which makes this workaround very fragile
cc @tchung1118 @jacoboaks
This issue has been resolved by #360