Hey,
i have a CustomError type and implemented the error
interface and the Unwrap()
method in the typical manner. Now lets say i have some 3 levels deep function call stack. All levels add some errors.
Level1 (deepest) wrap some io error by doing:
fmt.Errorf(„err occured in level1 %w“, ioErr)
Level2 wrap above returned level1 err into my custom error:
return NewCustomError(„err occurred in level2, lvl1Err)
Level3 wrap my above returned custom error again into a „normal“ go error, by doing:
fmt.Errorf(„err occured in level3 %w“, customErr)
In main()
i do now some fmt.Println(err)
of level3 error. But the printed chain just shows the level3 error and level2 error. As soon as Println()
„hits“ my custom error it stops „chaining of print“.
The following i did:
-
If i replace my custom error in level2 with also a
fmt.Errorf()
call, the main‘sfmt.Println()
prints all errors of all levels. -
If i stop debugger in
main()
, i can clearly see, even with using custom error, that all errors are existing in the chain. -
if i use
errors.Unwrap()
of golang, i can manually unwrap and print all errors, including my custom error.
In short: Every error is chained correctly, but for whatever reason fmt.Println(err)
stops wrapped printing, when reaching the custom error.
I always thought fmt.Println()
does the same thing, i did in point3 above: Hangling through all errors, by calling Unwrap()
and printing the error until it is nil. Instead it stops when reaching a custom error. And i want to know why?
You can find simple sample code, for better understanding, here:
Any help is very appreciated,
best wishes