Join errors feature is not avaliable at go 1.19
This commit is contained in:
parent
46d854d0e9
commit
6a88132acf
1 changed files with 4 additions and 29 deletions
29
error.go
29
error.go
|
@ -4,27 +4,6 @@ import(
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type unwraperr interface {
|
|
||||||
Unwrap() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type unwraperrs interface {
|
|
||||||
Unwrap() []error
|
|
||||||
}
|
|
||||||
|
|
||||||
func isUnwrapable(err error) bool {
|
|
||||||
_, ok1 := err.(unwraperr)
|
|
||||||
_, ok2 := err.(unwraperrs)
|
|
||||||
return ok1 || ok2
|
|
||||||
}
|
|
||||||
|
|
||||||
func enshureUnwrapable(err error) error {
|
|
||||||
if isUnwrapable(err) {
|
|
||||||
return fmt.Errorf(err.Error())
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func filterNilErrors(errs []error) (notNilErrs []error) {
|
func filterNilErrors(errs []error) (notNilErrs []error) {
|
||||||
for _, err := range errs {
|
for _, err := range errs {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -41,16 +20,12 @@ func Join(errs ...error) (result error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for index, err := range errs {
|
for index, err := range errs {
|
||||||
fmt.Println(isUnwrapable(err), err.Error())
|
|
||||||
err = enshureUnwrapable(err)
|
|
||||||
fmt.Println(isUnwrapable(err), err.Error())
|
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
result = err
|
result = err
|
||||||
} else {
|
} else {
|
||||||
result = fmt.Errorf("%w: %w", result, fmt.Errorf("foo"))
|
result = fmt.Errorf("%w: %v", result, err)
|
||||||
fmt.Println("1: ",result)
|
|
||||||
fmt.Println("2: ",err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue