Skip to content

Commit c4dc1ee

Browse files
committed
use unwrapping of errors in push for consistency
1 parent ce1a7c4 commit c4dc1ee

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

push/errors.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,31 @@ func IsHandlerNilError(err error) bool {
145145
return errors.Is(err, ErrHandlerNil)
146146
}
147147

148-
// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler
148+
// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler.
149+
// This function works correctly even when the error is wrapped.
149150
func IsHandlerExistsError(err error) bool {
150-
if handlerErr, ok := err.(*HandlerError); ok {
151+
var handlerErr *HandlerError
152+
if errors.As(err, &handlerErr) {
151153
return handlerErr.Operation == ProcessorOperationRegister && handlerErr.Reason == ReasonHandlerExists
152154
}
153155
return false
154156
}
155157

156-
// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler
158+
// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler.
159+
// This function works correctly even when the error is wrapped.
157160
func IsProtectedHandlerError(err error) bool {
158-
if handlerErr, ok := err.(*HandlerError); ok {
161+
var handlerErr *HandlerError
162+
if errors.As(err, &handlerErr) {
159163
return handlerErr.Operation == ProcessorOperationUnregister && handlerErr.Reason == ReasonHandlerProtected
160164
}
161165
return false
162166
}
163167

164-
// IsVoidProcessorError checks if an error is due to void processor operations
168+
// IsVoidProcessorError checks if an error is due to void processor operations.
169+
// This function works correctly even when the error is wrapped.
165170
func IsVoidProcessorError(err error) bool {
166-
if procErr, ok := err.(*ProcessorError); ok {
171+
var procErr *ProcessorError
172+
if errors.As(err, &procErr) {
167173
return procErr.ProcessorType == ProcessorTypeVoidProcessor && procErr.Reason == ReasonPushNotificationsDisabled
168174
}
169175
return false

push/processor_unit_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package push
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67
)
78

@@ -313,3 +314,85 @@ func (h *UnitTestHandler) Reset() {
313314
h.lastNotification = nil
314315
h.errorToReturn = nil
315316
}
317+
318+
// TestErrorWrapping tests that error checking functions work with wrapped errors
319+
func TestErrorWrapping(t *testing.T) {
320+
t.Run("IsHandlerExistsError with wrapped error", func(t *testing.T) {
321+
// Create a HandlerError
322+
handlerErr := ErrHandlerExists("test-notification")
323+
324+
// Wrap it
325+
wrappedErr := fmt.Errorf("operation failed: %w", handlerErr)
326+
doubleWrappedErr := fmt.Errorf("context: %w", wrappedErr)
327+
328+
// Should still be detected through wrapping
329+
if !IsHandlerExistsError(doubleWrappedErr) {
330+
t.Errorf("IsHandlerExistsError should detect wrapped error")
331+
}
332+
333+
// Verify it doesn't match other error types
334+
if IsProtectedHandlerError(doubleWrappedErr) {
335+
t.Errorf("IsProtectedHandlerError should not match handler exists error")
336+
}
337+
})
338+
339+
t.Run("IsProtectedHandlerError with wrapped error", func(t *testing.T) {
340+
// Create a protected handler error
341+
protectedErr := ErrProtectedHandler("protected-notification")
342+
343+
// Wrap it
344+
wrappedErr := fmt.Errorf("unregister failed: %w", protectedErr)
345+
346+
// Should still be detected through wrapping
347+
if !IsProtectedHandlerError(wrappedErr) {
348+
t.Errorf("IsProtectedHandlerError should detect wrapped error")
349+
}
350+
351+
// Verify it doesn't match other error types
352+
if IsHandlerExistsError(wrappedErr) {
353+
t.Errorf("IsHandlerExistsError should not match protected handler error")
354+
}
355+
})
356+
357+
t.Run("IsVoidProcessorError with wrapped error", func(t *testing.T) {
358+
// Create a void processor error
359+
voidErr := ErrVoidProcessorRegister("test-notification")
360+
361+
// Wrap it multiple times
362+
wrappedErr := fmt.Errorf("register failed: %w", voidErr)
363+
doubleWrappedErr := fmt.Errorf("processor error: %w", wrappedErr)
364+
365+
// Should still be detected through wrapping
366+
if !IsVoidProcessorError(doubleWrappedErr) {
367+
t.Errorf("IsVoidProcessorError should detect wrapped error")
368+
}
369+
})
370+
371+
t.Run("IsHandlerNilError with wrapped error", func(t *testing.T) {
372+
// Wrap the nil handler error
373+
wrappedErr := fmt.Errorf("validation failed: %w", ErrHandlerNil)
374+
375+
// Should still be detected through wrapping
376+
if !IsHandlerNilError(wrappedErr) {
377+
t.Errorf("IsHandlerNilError should detect wrapped error")
378+
}
379+
})
380+
381+
t.Run("Error functions return false for non-matching errors", func(t *testing.T) {
382+
// Create a different error
383+
otherErr := fmt.Errorf("some other error")
384+
385+
if IsHandlerExistsError(otherErr) {
386+
t.Errorf("IsHandlerExistsError should return false for non-matching error")
387+
}
388+
if IsProtectedHandlerError(otherErr) {
389+
t.Errorf("IsProtectedHandlerError should return false for non-matching error")
390+
}
391+
if IsVoidProcessorError(otherErr) {
392+
t.Errorf("IsVoidProcessorError should return false for non-matching error")
393+
}
394+
if IsHandlerNilError(otherErr) {
395+
t.Errorf("IsHandlerNilError should return false for non-matching error")
396+
}
397+
})
398+
}

0 commit comments

Comments
 (0)