-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from savaki/master
errors.Errorf preserves original error similar to fmt.Error
- Loading branch information
Showing
3 changed files
with
78 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
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,55 @@ | ||
package errors | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
) | ||
|
||
// Is is simplified facsimile of the go 1.13 errors.Is to ensure QueryError is compatible | ||
func Is(err, target error) bool { | ||
for err != nil { | ||
if target == err { | ||
return true | ||
} | ||
|
||
switch e := err.(type) { | ||
case interface{ Unwrap() error }: | ||
err = e.Unwrap() | ||
default: | ||
break | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func TestErrorf(t *testing.T) { | ||
cause := io.EOF | ||
|
||
t.Run("wrap error", func(t *testing.T) { | ||
err := Errorf("boom: %v", cause) | ||
if !Is(err, cause) { | ||
t.Fatalf("expected errors.Is to return true") | ||
} | ||
}) | ||
|
||
t.Run("handles nil", func(t *testing.T) { | ||
var err *QueryError | ||
if Is(err, cause) { | ||
t.Fatalf("expected errors.Is to return false") | ||
} | ||
}) | ||
|
||
t.Run("handle no arguments", func(t *testing.T) { | ||
err := Errorf("boom") | ||
if Is(err, cause) { | ||
t.Fatalf("expected errors.Is to return false") | ||
} | ||
}) | ||
|
||
t.Run("handle non-error argument arguments", func(t *testing.T) { | ||
err := Errorf("boom: %v", "shaka") | ||
if Is(err, cause) { | ||
t.Fatalf("expected errors.Is to return false") | ||
} | ||
}) | ||
} |
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