Skip to content
Myr

implement ".Unwrap() error" on Error type (#2525) (#2526)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Georges Varouchas · committed by GitHub · parent c83a1cc · Browse files · 2 files+38 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Co-authored-by: thinkerou <thinkerou@gmail.com>

Diff stats

2 files+38 added−0 removed

  • other:+38 added−0 removed

2 files+38 added−0 removed

added:errors_1.13_test.go+33 added
1// +build go1.1323package gin45import (6 "errors"7 "fmt"8 "testing"910 "github.com/stretchr/testify/assert"11)1213type TestErr string1415func (e TestErr) Error() string { return string(e) }1617// TestErrorUnwrap tests the behavior of gin.Error with "errors.Is()" and "errors.As()".18// "errors.Is()" and "errors.As()" have been added to the standard library in go 1.13,19// hence the "// +build go1.13" directive at the beginning of this file.20func TestErrorUnwrap(t *testing.T) {21 innerErr := TestErr("somme error")2223 // 2 layers of wrapping : use 'fmt.Errorf("%w")' to wrap a gin.Error{}, which itself wraps innerErr24 err := fmt.Errorf("wrapped: %w", &Error{25 Err: innerErr,26 Type: ErrorTypeAny,27 })2829 // check that 'errors.Is()' and 'errors.As()' behave as expected :30 assert.True(t, errors.Is(err, innerErr))31 var testErr TestErr32 assert.True(t, errors.As(err, &testErr))33}
modified:errors.go+5 added
88 unmodified lines
89func (msg *Error) IsType(flags ErrorType) bool {90 return (msg.Type & flags) > 091}9293// Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap()94func (msg *Error) Unwrap() error {95 return msg.Err96}9798// ByType returns a readonly copy filtered the byte.99// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.100func (a errorMsgs) ByType(typ ErrorType) errorMsgs {101 if len(a) == 0 {
73 unmodified lines