forked from gin-gonic/gin
-
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.
implement ".Unwrap() error" on Error type (gin-gonic#2525) (gin-gonic…
…#2526) Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: thinkerou <thinkerou@gmail.com>
- Loading branch information
1 parent
c83a1cc
commit f969bfa
Showing
2 changed files
with
38 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,33 @@ | ||
// +build go1.13 | ||
|
||
package gin | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestErr string | ||
|
||
func (e TestErr) Error() string { return string(e) } | ||
|
||
// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()". | ||
// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13, | ||
// hence the "// +build go1.13" directive at the beginning of this file. | ||
func TestErrorUnwrap(t *testing.T) { | ||
innerErr := TestErr("somme error") | ||
|
||
// 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr | ||
err := fmt.Errorf("wrapped: %w", &Error{ | ||
Err: innerErr, | ||
Type: ErrorTypeAny, | ||
}) | ||
|
||
// check that 'errors.Is()' and 'errors.As()' behave as expected : | ||
assert.True(t, errors.Is(err, innerErr)) | ||
var testErr TestErr | ||
assert.True(t, errors.As(err, &testErr)) | ||
} |