Hi Alex, I see your problem is more like merging errors rather than wrapping them. Indeed I don't see any way you could merge 2 errors with the new ` *fmt.Errorf*`
I believe what you want is something which could hold 2 errors, like type myError struct { msg string // some additional msg current error // Layer1Error cause error // Layer2Error } but in that case, you are handling it by yourself, instead of delegating it to fmt.Errorf. What I see as a possibility would be something like: var ( Layer1Error = errors.New("some error on layer 1: %w") Layer2Error = errors.New("some error on layer 2") ) func callLayer1Function() error { err := callLayer2Function() // how to not lose layer2 error but also append a new layer1 error ? // this does not work, since you fully lose layer1 error // with pkg/err return fmt.Errorf(Layer1Error.Error(), Layer1Error) } However it makes pointles to have `Layer1Error` as an error, it can be juts a string. Best, Anderson On Friday, 9 August 2019 15:39:24 UTC+2, Alex wrote: > > Sorry, mixed things up in the code with layer1 and layer2... > > > var ( > Layer1Error = errors.New("some error on layer 1") > Layer2Error = errors.New("some error on layer 2") > ) > > func main() { > err := callLayer1Function() > > // do something with error > } > > func callLayer1Function() error { > err := callLayer2Function() > > // how to not lose layer2 error but also append a new layer1 error ? > // this does not work, since you fully lose layer1 error > // with pkg/err > return fmt.Errorf("some specific layer 1 error message: %w", Layer1Error) > } > > func callLayer2Function() error { > // wrap an error of Layer2 here > return fmt.Errorf("some specific layer2 error message: %w", Layer2Error) > } > > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/08858872-57b1-45a0-86f9-5339a10592fb%40googlegroups.com.