-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
6 changed files
with
71 additions
and
20 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
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,19 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package errors | ||
|
||
import stderrors "errors" | ||
|
||
// Join allows this library to be a drop-in replacement to the std library. | ||
// | ||
// 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. | ||
// | ||
// It is the responsibility of the caller to then check for nil and wrap this error if desired. | ||
func Join(errs ...error) error { | ||
return stderrors.Join(errs...) | ||
} |
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,39 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package errors | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestJoin(t *testing.T) { | ||
err1 := io.EOF | ||
err2 := io.ErrUnexpectedEOF | ||
|
||
err := Join(err1, err2) | ||
innerErr, ok := err.(interface{ Unwrap() []error }) | ||
if !ok { | ||
t.Fatalf("expected Join to return an error that implements Unwrap() []error") | ||
} | ||
errs := innerErr.Unwrap() | ||
if len(errs) != 2 { | ||
t.Fatalf("expected Join to return an error that implements Unwrap() []error to return 2 errors") | ||
} | ||
if !Is(errs[0], io.EOF) { | ||
t.Fatalf("expected Join to return an error that implements Unwrap() []error to return io.EOF") | ||
} | ||
if !Is(errs[1], io.ErrUnexpectedEOF) { | ||
t.Fatalf("expected Join to return an error that implements Unwrap() []error to return io.ErrUnexpectedEOF") | ||
} | ||
|
||
// test wrapping and then unwrapping with Chain | ||
err = Wrap(err, "my test wrapped error") | ||
if !Is(err, io.EOF) { | ||
t.Fatalf("expected wrapped error to traverse into joined inner error EOF") | ||
} | ||
if !Is(err, io.ErrUnexpectedEOF) { | ||
t.Fatalf("expected wrapped error to traverse into joined inner error ErrUnexpectedEOF") | ||
} | ||
} |
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