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/497b6f13-a4de-4780-904b-12b1fa53a8ef%40googlegroups.com.

Reply via email to