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/7a97e612-132c-46ac-ad6f-33a3c19632f8%40googlegroups.com.

Reply via email to