-
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.
issues/443: errors; add equivalent functions from standard library (#449
) - With this, you only need to inport ong/errors without needing to import standard lib - Fixes: #443
- Loading branch information
Showing
10 changed files
with
282 additions
and
23 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
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,65 @@ | ||
package errors | ||
|
||
// Some of the code here is inspired(or taken from) by: | ||
// (a) https://github.com/golang/go/blob/go1.20.14/src/errors/join.go whose license(BSD 3-Clause) can be found here: https://github.com/golang/go/blob/go1.20.14/LICENSE | ||
|
||
// 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 only returns the stack trace of the first error. Unwrap also only returns the first error. | ||
// | ||
// Note that this function is equivalent to the one in standard library only in spirit. | ||
// This is not a direct replacement of the standard library one. | ||
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 { | ||
ef := wrap(err, 3) | ||
e.errs = append(e.errs, ef) | ||
if e.stackError == nil { | ||
e.stackError = ef | ||
} | ||
} | ||
} | ||
|
||
return e | ||
} | ||
|
||
type joinError struct { | ||
*stackError | ||
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 { | ||
if len(e.errs) > 0 { | ||
return e.errs[0] | ||
} | ||
return nil | ||
} |
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,129 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package errors | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"go.akshayshah.org/attest" | ||
) | ||
|
||
// Some of the code here is inspired(or taken from) by: | ||
// (a) https://github.com/golang/go/blob/go1.20.14/src/errors/join.go whose license(BSD 3-Clause) can be found here: https://github.com/golang/go/blob/go1.20.14/LICENSE | ||
|
||
func TestJoinReturnsNil(t *testing.T) { | ||
if err := Join(); err != nil { | ||
t.Errorf("errors.Join() = %v, want nil", err) | ||
} | ||
if err := Join(nil); err != nil { | ||
t.Errorf("errors.Join(nil) = %v, want nil", err) | ||
} | ||
if err := Join(nil, nil); err != nil { | ||
t.Errorf("errors.Join(nil, nil) = %v, want nil", err) | ||
} | ||
} | ||
|
||
func TestJoin(t *testing.T) { | ||
err1 := New("err1") | ||
err2 := New("err2") | ||
for _, test := range []struct { | ||
errs []error | ||
want error | ||
}{ | ||
{ | ||
errs: []error{err1}, | ||
want: err1, | ||
}, | ||
{ | ||
errs: []error{err1, err2}, | ||
want: err1, | ||
}, | ||
{ | ||
errs: []error{err2, err1, nil}, | ||
want: err2, | ||
}, | ||
{ | ||
errs: []error{nil, err2, err1}, | ||
want: err2, | ||
}, | ||
} { | ||
got := Join(test.errs...).(interface{ Unwrap() error }).Unwrap() | ||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("Join(%v) got = %v; want %v", test.errs, got, test.want) | ||
} | ||
// if len(got) != cap(got) { | ||
// t.Errorf("Join(%v) returns errors with len=%v, cap=%v; want len==cap", test.errs, len(got), cap(got)) | ||
// } | ||
} | ||
} | ||
|
||
func TestJoinErrorMethod(t *testing.T) { | ||
err1 := New("err1") | ||
err2 := New("err2") | ||
for _, test := range []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", | ||
}} { | ||
got := Join(test.errs...).Error() | ||
if got != test.want { | ||
t.Errorf("Join(%v).Error() = %q; want %q", test.errs, got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func TestJoinStackTrace(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("errors.Join", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err1 := New("hello") | ||
err2 := hello() | ||
|
||
{ | ||
err3 := Join(err1, err2) | ||
|
||
sterr, ok := err3.(*joinError) | ||
attest.True(t, ok) | ||
attest.Equal(t, sterr.Error(), "hello\nerror in foo") | ||
|
||
stackTrace := sterr.getStackTrace() | ||
for _, v := range []string{ | ||
"ong/errors/join_test.go:92", // Join only shows stack trace of first error. ie, err1 | ||
} { | ||
attest.Subsequence(t, stackTrace, v, attest.Sprintf("\n\t%s: not found in stackTrace: %s", v, stackTrace)) | ||
} | ||
} | ||
|
||
{ | ||
err3 := Join(err2, err1) | ||
|
||
sterr, ok := err3.(*joinError) | ||
attest.True(t, ok) | ||
attest.Equal(t, sterr.Error(), "error in foo\nhello") | ||
|
||
stackTrace := sterr.getStackTrace() | ||
for _, v := range []string{ | ||
// Join only shows stack trace of first error. ie, err2 | ||
"ong/errors/errors_test.go:30", | ||
"ong/errors/errors_test.go:23", | ||
"ong/errors/errors_test.go:17", | ||
"ong/errors/join_test.go:93", | ||
} { | ||
attest.Subsequence(t, stackTrace, v, attest.Sprintf("\n\t%s: not found in stackTrace: %s", v, stackTrace)) | ||
} | ||
} | ||
}) | ||
} |
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,26 @@ | ||
package errors | ||
|
||
import ( | ||
stdErrors "errors" | ||
"fmt" | ||
) | ||
|
||
// As is a pass through to the same func from the standard library errors package. | ||
func As(err error, target any) bool { | ||
return stdErrors.As(err, target) | ||
} | ||
|
||
// Is is a pass through to the same func from the standard library errors package. | ||
func Is(err, target error) bool { | ||
return stdErrors.Is(err, target) | ||
} | ||
|
||
// Unwrap is a pass through to the same func from the standard library errors package. | ||
func Unwrap(err error) error { | ||
return stdErrors.Unwrap(err) | ||
} | ||
|
||
// Errorf is a pass through to the same func from the standard library fmt package. | ||
func Errorf(format string, a ...any) error { | ||
return fmt.Errorf(format, a...) | ||
} |
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,26 @@ | ||
package errors | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"testing" | ||
|
||
"go.akshayshah.org/attest" | ||
) | ||
|
||
func TestStdLib(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("stdlib pass throughs", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := prepFile() | ||
var targetErr *fs.PathError | ||
|
||
_, ok := err.(*stackError) | ||
attest.True(t, ok) | ||
attest.True(t, Is(err, os.ErrNotExist)) | ||
attest.NotZero(t, Unwrap(err)) | ||
attest.True(t, As(err, &targetErr)) | ||
}) | ||
} |
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.