-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added
Join
for compatible with go1.20
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright © 2023 Hedzr Yeh. | ||
|
||
package errors | ||
|
||
// Join returns an error that wraps the given errors. | ||
// Any nil error values are discarded. | ||
// Join returns nil if errs contains no non-nil values. | ||
// 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. | ||
func Join(errs ...error) error { | ||
return New().WithErrors(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,42 @@ | ||
// Copyright © 2023 Hedzr Yeh. | ||
|
||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package errors_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestJoinErrorsStd(t *testing.T) { | ||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
err := errors.Join(err1, err2) | ||
fmt.Printf("%T, %v\n", err, err) | ||
if errors.Is(err, err1) { | ||
t.Log("err is err1") | ||
} else { | ||
t.Fatal("expecting err is err1") | ||
} | ||
if errors.Is(err, err2) { | ||
t.Log("err is err2") | ||
} else { | ||
t.Fatal("expecting err is err2") | ||
} | ||
|
||
err3 := fmt.Errorf("error3: %w", err) | ||
fmt.Printf("%T, %v\n", err3, errors.Unwrap(err3)) | ||
if errors.Is(err3, err1) { | ||
t.Log("err3 is err1") | ||
} else { | ||
t.Fatal("expecting err3 is err1") | ||
} | ||
if errors.Is(err3, err2) { | ||
t.Log("err3 is err2") | ||
} else { | ||
t.Fatal("expecting err3 is err1") | ||
} | ||
} |
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,40 @@ | ||
// Copyright © 2023 Hedzr Yeh. | ||
|
||
package errors_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"gopkg.in/hedzr/errors.v3" | ||
) | ||
|
||
func TestJoinErrors(t *testing.T) { | ||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
err := errors.Join(err1, err2) | ||
fmt.Printf("%T, %v\n", err, err) | ||
if errors.Is(err, err1) { | ||
t.Log("err is err1") | ||
} else { | ||
t.Fatal("expecting err is err1") | ||
} | ||
if errors.Is(err, err2) { | ||
t.Log("err is err2") | ||
} else { | ||
t.Fatal("expecting err is err2") | ||
} | ||
|
||
err3 := fmt.Errorf("error3: %w", err) | ||
fmt.Printf("%T, %v\n", err3, errors.Unwrap(err3)) | ||
if errors.Is(err3, err1) { | ||
t.Log("err3 is err1") | ||
} else { | ||
t.Fatal("expecting err3 is err1") | ||
} | ||
if errors.Is(err3, err2) { | ||
t.Log("err3 is err2") | ||
} else { | ||
t.Fatal("expecting err3 is err1") | ||
} | ||
} |