-
Notifications
You must be signed in to change notification settings - Fork 0
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
21bcd07
commit d486efa
Showing
8 changed files
with
318 additions
and
5 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/muonsoft/errors | ||
|
||
go 1.18 | ||
go 1.20 | ||
|
||
require github.com/sirupsen/logrus v1.8.1 | ||
|
||
|
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 @@ | ||
package errors | ||
|
||
// Join returns an error that wraps the given errors with a stack trace | ||
// at the point Join is called. 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. | ||
// If there is only one error in chain, then it's stack trace will be | ||
// preserved if present. | ||
func Join(errs ...error) error { | ||
n := 0 | ||
for _, err := range errs { | ||
if err != nil { | ||
n++ | ||
} | ||
} | ||
if n == 0 { | ||
return nil | ||
} | ||
if n == 1 { | ||
for _, err := range errs { | ||
if err != nil { | ||
if isWrapper(err) { | ||
return err | ||
} | ||
|
||
return &stacked{ | ||
wrapped: &wrapped{wrapped: err}, | ||
stack: newStack(0), | ||
} | ||
} | ||
} | ||
} | ||
|
||
e := &joinError{errs: make([]error, 0, n)} | ||
|
||
for _, err := range errs { | ||
if err != nil { | ||
e.errs = append(e.errs, err) | ||
} | ||
} | ||
|
||
return &stacked{ | ||
wrapped: &wrapped{wrapped: e}, | ||
stack: newStack(0), | ||
} | ||
} | ||
|
||
type joinError struct { | ||
errs []error | ||
} | ||
|
||
// todo: add marshal json? | ||
|
||
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 | ||
} |
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,82 @@ | ||
package errors_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/muonsoft/errors" | ||
) | ||
|
||
func TestJoin_ReturnsNil(t *testing.T) { | ||
if err := errors.Join(); err != nil { | ||
t.Errorf("errors.Join() = %v, want nil", err) | ||
} | ||
if err := errors.Join(nil); err != nil { | ||
t.Errorf("errors.Join(nil) = %v, want nil", err) | ||
} | ||
if err := errors.Join(nil, nil); err != nil { | ||
t.Errorf("errors.Join(nil, nil) = %v, want nil", err) | ||
} | ||
} | ||
|
||
func TestJoin(t *testing.T) { | ||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
tests := []struct { | ||
errs []error | ||
want []error | ||
}{ | ||
{ | ||
errs: []error{err1}, | ||
want: []error{err1}, | ||
}, | ||
{ | ||
errs: []error{err1, err2}, | ||
want: []error{err1, err2}, | ||
}, | ||
{ | ||
errs: []error{err1, nil, err2}, | ||
want: []error{err1, err2}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(fmt.Sprintf("%v", test.errs), func(t *testing.T) { | ||
got := errors.Join(test.errs...) | ||
for _, want := range test.want { | ||
if !errors.Is(got, want) { | ||
t.Errorf("want err %v in chain", want) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestJoin_ErrorMethod(t *testing.T) { | ||
err1 := errors.New("err1") | ||
err2 := errors.New("err2") | ||
tests := []struct { | ||
errs []error | ||
want string | ||
}{ | ||
{ | ||
errs: []error{err1}, | ||
want: "err1", | ||
}, | ||
{ | ||
errs: []error{err1, err2}, | ||
want: "err1\nerr2", | ||
}, | ||
{ | ||
errs: []error{err1, nil, err2}, | ||
want: "err1\nerr2", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(fmt.Sprintf("%v", test.errs), func(t *testing.T) { | ||
got := errors.Join(test.errs...).Error() | ||
if got != test.want { | ||
t.Errorf("Join().Error() = %q; want %q", got, test.want) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.