-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9cbc55
commit 5db40d6
Showing
4 changed files
with
105 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ language: go | |
|
||
go: | ||
- "1.8.x" | ||
- "1.10.x" | ||
- "1.13.x" | ||
- "1.14.x" | ||
- "1.11.x" | ||
- "1.16.x" | ||
- "1.21.x" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package errors | ||
|
||
import baseErrors "errors" | ||
|
||
// Join returns an error that wraps the given errors. | ||
// Any nil error values are discarded. | ||
// Join returns nil if every value in errs is nil. | ||
// The error formats as the concatenation of the strings obtained | ||
// by calling the Error method of each element of errs, with a newline | ||
// between each string. | ||
// | ||
// A non-nil error returned by Join implements the Unwrap() []error method. | ||
// | ||
// For more information see stdlib errors.Join. | ||
func Join(errs ...error) error { | ||
return baseErrors.Join(errs...) | ||
} | ||
|
||
// Unwrap returns the result of calling the Unwrap method on err, if err's | ||
// type contains an Unwrap method returning error. | ||
// Otherwise, Unwrap returns nil. | ||
// | ||
// Unwrap only calls a method of the form "Unwrap() error". | ||
// In particular Unwrap does not unwrap errors returned by [Join]. | ||
// | ||
// For more information see stdlib errors.Unwrap. | ||
func Unwrap(err error) error { | ||
return baseErrors.Unwrap(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//go:build !go1.20 | ||
// +build !go1.20 | ||
|
||
package errors | ||
|
||
// Disclaimer: functions Join and Unwrap are copied from the stdlib errors | ||
// package v1.21.0. | ||
|
||
// Join returns an error that wraps the given errors. | ||
// Any nil error values are discarded. | ||
// Join returns nil if every value in errs is nil. | ||
// The error formats as the concatenation of the strings obtained | ||
// by calling the Error method of each element of errs, with a newline | ||
// between each string. | ||
// | ||
// A non-nil error returned by Join implements the Unwrap() []error method. | ||
func Join(errs ...error) error { | ||
n := 0 | ||
for _, err := range errs { | ||
if err != nil { | ||
n++ | ||
} | ||
} | ||
if n == 0 { | ||
return nil | ||
} | ||
e := &joinError{ | ||
errs: make([]error, 0, n), | ||
} | ||
for _, err := range errs { | ||
if err != nil { | ||
e.errs = append(e.errs, err) | ||
} | ||
} | ||
return e | ||
} | ||
|
||
type joinError struct { | ||
errs []error | ||
} | ||
|
||
func (e *joinError) Error() string { | ||
var b []byte | ||
for i, err := range e.errs { | ||
if i > 0 { | ||
b = append(b, '\n') | ||
} | ||
b = append(b, err.Error()...) | ||
} | ||
return string(b) | ||
} | ||
|
||
func (e *joinError) Unwrap() []error { | ||
return e.errs | ||
} | ||
|
||
// Unwrap returns the result of calling the Unwrap method on err, if err's | ||
// type contains an Unwrap method returning error. | ||
// Otherwise, Unwrap returns nil. | ||
// | ||
// Unwrap only calls a method of the form "Unwrap() error". | ||
// In particular Unwrap does not unwrap errors returned by [Join]. | ||
func Unwrap(err error) error { | ||
u, ok := err.(interface { | ||
Unwrap() error | ||
}) | ||
if !ok { | ||
return nil | ||
} | ||
return u.Unwrap() | ||
} |