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

This commit is contained in:
Orkhan Alikhanov 2025-05-08 09:24:01 +00:00
parent 67c9d4ee11
commit f533f2a698
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)
} }