Compare commits

..

1 commit
v0.3.1 ... main

Author SHA1 Message Date
34a01c6279 Pass through try-compatible errors
All checks were successful
continuous-integration/drone/push Build is passing
2021-10-17 10:59:33 +02:00
2 changed files with 25 additions and 2 deletions

View file

@ -47,7 +47,14 @@ type call struct {
}
// FromErr wraps a standard error into a try-compatible one with extended stack info.
func FromErr(err error) *tryErr {
func FromErr(err error) Err {
// If the error is already try-compatible, return
//nolint:errorlint
cterr, ok := err.(Err)
if ok {
return cterr
}
terr := &tryErr{err: err, callStack: []call{}}
pc, _, _, ok := runtime.Caller(0)
@ -82,7 +89,7 @@ func FromErr(err error) *tryErr {
}
// NewErr creates a new try-compatible error with extended stack info.
func NewErr(msg string) *tryErr {
func NewErr(msg string) Err {
return FromErr(errors.New(msg))
}

View file

@ -220,6 +220,22 @@ func TestReturnStd(t *testing.T) {
}
}
func TestCheckTryErr(t *testing.T) {
testErr := NewErr("TestErr")
tf := func() (err Err) {
defer Return(&err)
Check(testErr)
return
}
err := tf()
if err != testErr {
t.Fail()
}
}
func ExampleReturn() {
var err Err
defer Return(&err)