fix(errors): change Unwrap method receiver to value type (#4232)

This commit is contained in:
Orkhan Alikhanov 2025-05-11 18:38:33 +04:00 committed by GitHub
parent cf32d2dcf8
commit b38c59de7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -91,7 +91,7 @@ func (msg *Error) IsType(flags ErrorType) bool {
} }
// Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap() // Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap()
func (msg *Error) Unwrap() error { func (msg Error) Unwrap() error {
return msg.Err return msg.Err
} }

View File

@ -126,4 +126,15 @@ func TestErrorUnwrap(t *testing.T) {
require.ErrorIs(t, err, innerErr) require.ErrorIs(t, err, innerErr)
var testErr TestErr var testErr TestErr
require.ErrorAs(t, err, &testErr) require.ErrorAs(t, err, &testErr)
// Test non-pointer usage of gin.Error
errNonPointer := Error{
Err: innerErr,
Type: ErrorTypeAny,
}
wrappedErr := fmt.Errorf("wrapped: %w", errNonPointer)
// Check that 'errors.Is()' and 'errors.As()' behave as expected for non-pointer usage
require.ErrorIs(t, wrappedErr, innerErr)
var testErrNonPointer TestErr
require.ErrorAs(t, wrappedErr, &testErrNonPointer)
} }