Can you show me how is it possible to do with pkg/errors ? It is not immediately apparent to me. The Causer interface is similar to the Unwrap interface and errors.Cause recursively unwraps an error until it finds one which does not implements the causer interface. So just with errors.WithMessage/Wrap, you can only unwrap an error, but cannot directly compare a nested untyped/unnamed error variable.
On Friday, 9 August 2019 23:21:57 UTC+5:30, Alex wrote: > > Yes, sure. This is always possible. :-) > > But this is kind of writing your own error wrapper. I was just wondering > if this is somehow possible with the new error wrapper like it was with > https://github.com/pkg/errors. > > Am Freitag, 9. August 2019 19:35:42 UTC+2 schrieb Agniva De Sarker: >> >> I see. One way is to create a wrapper error type in layer1, which takes a >> layer2 error. Just like os.PathError. >> >> package main >> >> import ( >> "errors" >> "fmt" >> ) >> >> var ( >> // Layer1Error = errors.New("some error on layer 1") >> Layer2Error = errors.New("some error on layer 2") >> ) >> >> type Layer1Error struct { >> internal error >> } >> >> func (le *Layer1Error) Error() string { >> return fmt.Sprintf("layer2 error: %v", le.internal) >> } >> >> func (le *Layer1Error) Unwrap() error { >> return le.internal >> } >> >> func main() { >> err := callLayer1Function() >> fmt.Println(errors.Is(err, Layer2Error)) >> var l2err *Layer1Error >> fmt.Println(errors.As(err, &l2err)) >> } >> >> func callLayer1Function() error { >> err := callLayer2Function() >> return &Layer1Error{err} >> } >> >> func callLayer2Function() error { >> // wrap an error of Layer2 here >> return fmt.Errorf("some specific layer2 error message: %w", Layer2Error) >> } >> >> >> On Friday, 9 August 2019 22:43:11 UTC+5:30, Alex wrote: >>> >>> Hi Agniva, >>> >>> the problem is: In the main function is no information that there was an >>> Layer2 error (layer2 error is not included in the error anymore). >>> I don't know how to take the error from layer2 and wrap another error >>> (layer1-error) around it. >>> >>> You can only use the verb "%w" once in a fmt.Errorf() function afaik. >>> So if you have a wrapped error object e1, how would you enrich / wrap >>> this with another error e2? >>> >>> >>> Thanks, >>> Alex >>> >> -- 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/b4b65eec-fcc9-457f-af2c-6ea99cabc49f%40googlegroups.com.