|
| 1 | +package push |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// Push notification error definitions |
| 10 | +// This file contains all error types and messages used by the push notification system |
| 11 | + |
| 12 | +// Common error variables for reuse |
| 13 | +var ( |
| 14 | + // ErrHandlerNil is returned when attempting to register a nil handler |
| 15 | + ErrHandlerNil = errors.New("handler cannot be nil") |
| 16 | +) |
| 17 | + |
| 18 | +// Registry errors |
| 19 | + |
| 20 | +// ErrHandlerExists creates an error for when attempting to overwrite an existing handler |
| 21 | +func ErrHandlerExists(pushNotificationName string) error { |
| 22 | + return fmt.Errorf("cannot overwrite existing handler for push notification: %s", pushNotificationName) |
| 23 | +} |
| 24 | + |
| 25 | +// ErrProtectedHandler creates an error for when attempting to unregister a protected handler |
| 26 | +func ErrProtectedHandler(pushNotificationName string) error { |
| 27 | + return fmt.Errorf("cannot unregister protected handler for push notification: %s", pushNotificationName) |
| 28 | +} |
| 29 | + |
| 30 | +// VoidProcessor errors |
| 31 | + |
| 32 | +// ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor |
| 33 | +func ErrVoidProcessorRegister(pushNotificationName string) error { |
| 34 | + return fmt.Errorf("cannot register push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName) |
| 35 | +} |
| 36 | + |
| 37 | +// ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor |
| 38 | +func ErrVoidProcessorUnregister(pushNotificationName string) error { |
| 39 | + return fmt.Errorf("cannot unregister push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName) |
| 40 | +} |
| 41 | + |
| 42 | +// Error message constants for consistency |
| 43 | +const ( |
| 44 | + // Error message templates |
| 45 | + MsgHandlerNil = "handler cannot be nil" |
| 46 | + MsgHandlerExists = "cannot overwrite existing handler for push notification: %s" |
| 47 | + MsgProtectedHandler = "cannot unregister protected handler for push notification: %s" |
| 48 | + MsgVoidProcessorRegister = "cannot register push notification handler '%s': push notifications are disabled (using void processor)" |
| 49 | + MsgVoidProcessorUnregister = "cannot unregister push notification handler '%s': push notifications are disabled (using void processor)" |
| 50 | +) |
| 51 | + |
| 52 | +// Error type definitions for advanced error handling |
| 53 | + |
| 54 | +// HandlerError represents errors related to handler operations |
| 55 | +type HandlerError struct { |
| 56 | + Operation string // "register", "unregister", "get" |
| 57 | + PushNotificationName string |
| 58 | + Reason string |
| 59 | + Err error |
| 60 | +} |
| 61 | + |
| 62 | +func (e *HandlerError) Error() string { |
| 63 | + if e.Err != nil { |
| 64 | + return fmt.Sprintf("handler %s failed for '%s': %s (%v)", e.Operation, e.PushNotificationName, e.Reason, e.Err) |
| 65 | + } |
| 66 | + return fmt.Sprintf("handler %s failed for '%s': %s", e.Operation, e.PushNotificationName, e.Reason) |
| 67 | +} |
| 68 | + |
| 69 | +func (e *HandlerError) Unwrap() error { |
| 70 | + return e.Err |
| 71 | +} |
| 72 | + |
| 73 | +// NewHandlerError creates a new HandlerError |
| 74 | +func NewHandlerError(operation, pushNotificationName, reason string, err error) *HandlerError { |
| 75 | + return &HandlerError{ |
| 76 | + Operation: operation, |
| 77 | + PushNotificationName: pushNotificationName, |
| 78 | + Reason: reason, |
| 79 | + Err: err, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// ProcessorError represents errors related to processor operations |
| 84 | +type ProcessorError struct { |
| 85 | + ProcessorType string // "processor", "void_processor" |
| 86 | + Operation string // "process", "register", "unregister" |
| 87 | + Reason string |
| 88 | + Err error |
| 89 | +} |
| 90 | + |
| 91 | +func (e *ProcessorError) Error() string { |
| 92 | + if e.Err != nil { |
| 93 | + return fmt.Sprintf("%s %s failed: %s (%v)", e.ProcessorType, e.Operation, e.Reason, e.Err) |
| 94 | + } |
| 95 | + return fmt.Sprintf("%s %s failed: %s", e.ProcessorType, e.Operation, e.Reason) |
| 96 | +} |
| 97 | + |
| 98 | +func (e *ProcessorError) Unwrap() error { |
| 99 | + return e.Err |
| 100 | +} |
| 101 | + |
| 102 | +// NewProcessorError creates a new ProcessorError |
| 103 | +func NewProcessorError(processorType, operation, reason string, err error) *ProcessorError { |
| 104 | + return &ProcessorError{ |
| 105 | + ProcessorType: processorType, |
| 106 | + Operation: operation, |
| 107 | + Reason: reason, |
| 108 | + Err: err, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Helper functions for common error scenarios |
| 113 | + |
| 114 | +// IsHandlerNilError checks if an error is due to a nil handler |
| 115 | +func IsHandlerNilError(err error) bool { |
| 116 | + return errors.Is(err, ErrHandlerNil) |
| 117 | +} |
| 118 | + |
| 119 | +// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler |
| 120 | +func IsHandlerExistsError(err error) bool { |
| 121 | + if err == nil { |
| 122 | + return false |
| 123 | + } |
| 124 | + return fmt.Sprintf("%v", err) == fmt.Sprintf(MsgHandlerExists, extractNotificationName(err)) |
| 125 | +} |
| 126 | + |
| 127 | +// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler |
| 128 | +func IsProtectedHandlerError(err error) bool { |
| 129 | + if err == nil { |
| 130 | + return false |
| 131 | + } |
| 132 | + return fmt.Sprintf("%v", err) == fmt.Sprintf(MsgProtectedHandler, extractNotificationName(err)) |
| 133 | +} |
| 134 | + |
| 135 | +// IsVoidProcessorError checks if an error is due to void processor operations |
| 136 | +func IsVoidProcessorError(err error) bool { |
| 137 | + if err == nil { |
| 138 | + return false |
| 139 | + } |
| 140 | + errStr := err.Error() |
| 141 | + return strings.Contains(errStr, "push notifications are disabled (using void processor)") |
| 142 | +} |
| 143 | + |
| 144 | +// extractNotificationName attempts to extract the notification name from error messages |
| 145 | +// This is a helper function for error type checking |
| 146 | +func extractNotificationName(err error) string { |
| 147 | + // This is a simplified implementation - in practice, you might want more sophisticated parsing |
| 148 | + // For now, we return a placeholder since the exact extraction logic depends on the error format |
| 149 | + return "unknown" |
| 150 | +} |
0 commit comments