dig icon indicating copy to clipboard operation
dig copied to clipboard

Ability to check if the error belongs to dig

Open AnatolyRugalev opened this issue 3 years ago • 3 comments

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 avatar Sep 12 '22 20:09 AnatolyRugalev

@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.

abhinav avatar Sep 12 '22 20:09 abhinav

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

AnatolyRugalev avatar Sep 12 '22 21:09 AnatolyRugalev

cc @tchung1118 @jacoboaks

sywhang avatar Sep 23 '22 16:09 sywhang

This issue has been resolved by #360

AnatolyRugalev avatar Nov 23 '22 16:11 AnatolyRugalev