mirror of
https://github.com/redis/go-redis.git
synced 2025-07-31 05:04:23 +03:00
fix(push): fix error checks
This commit is contained in:
@ -3,7 +3,6 @@ package push
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Push notification error definitions
|
// Push notification error definitions
|
||||||
@ -19,24 +18,24 @@ var (
|
|||||||
|
|
||||||
// ErrHandlerExists creates an error for when attempting to overwrite an existing handler
|
// ErrHandlerExists creates an error for when attempting to overwrite an existing handler
|
||||||
func ErrHandlerExists(pushNotificationName string) error {
|
func ErrHandlerExists(pushNotificationName string) error {
|
||||||
return fmt.Errorf("cannot overwrite existing handler for push notification: %s", pushNotificationName)
|
return NewHandlerError("register", pushNotificationName, "cannot overwrite existing handler", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrProtectedHandler creates an error for when attempting to unregister a protected handler
|
// ErrProtectedHandler creates an error for when attempting to unregister a protected handler
|
||||||
func ErrProtectedHandler(pushNotificationName string) error {
|
func ErrProtectedHandler(pushNotificationName string) error {
|
||||||
return fmt.Errorf("cannot unregister protected handler for push notification: %s", pushNotificationName)
|
return NewHandlerError("unregister", pushNotificationName, "handler is protected", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VoidProcessor errors
|
// VoidProcessor errors
|
||||||
|
|
||||||
// ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor
|
// ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor
|
||||||
func ErrVoidProcessorRegister(pushNotificationName string) error {
|
func ErrVoidProcessorRegister(pushNotificationName string) error {
|
||||||
return fmt.Errorf("cannot register push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName)
|
return NewProcessorError("void_processor", "register", "push notifications are disabled", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor
|
// ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor
|
||||||
func ErrVoidProcessorUnregister(pushNotificationName string) error {
|
func ErrVoidProcessorUnregister(pushNotificationName string) error {
|
||||||
return fmt.Errorf("cannot unregister push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName)
|
return NewProcessorError("void_processor", "unregister", "push notifications are disabled", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error message constants for consistency
|
// Error message constants for consistency
|
||||||
@ -118,33 +117,37 @@ func IsHandlerNilError(err error) bool {
|
|||||||
|
|
||||||
// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler
|
// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler
|
||||||
func IsHandlerExistsError(err error) bool {
|
func IsHandlerExistsError(err error) bool {
|
||||||
if err == nil {
|
if handlerErr, ok := err.(*HandlerError); ok {
|
||||||
return false
|
return handlerErr.Operation == "register" && handlerErr.Reason == "cannot overwrite existing handler"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%v", err) == fmt.Sprintf(MsgHandlerExists, extractNotificationName(err))
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler
|
// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler
|
||||||
func IsProtectedHandlerError(err error) bool {
|
func IsProtectedHandlerError(err error) bool {
|
||||||
if err == nil {
|
if handlerErr, ok := err.(*HandlerError); ok {
|
||||||
return false
|
return handlerErr.Operation == "unregister" && handlerErr.Reason == "handler is protected"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%v", err) == fmt.Sprintf(MsgProtectedHandler, extractNotificationName(err))
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsVoidProcessorError checks if an error is due to void processor operations
|
// IsVoidProcessorError checks if an error is due to void processor operations
|
||||||
func IsVoidProcessorError(err error) bool {
|
func IsVoidProcessorError(err error) bool {
|
||||||
if err == nil {
|
if procErr, ok := err.(*ProcessorError); ok {
|
||||||
return false
|
return procErr.ProcessorType == "void_processor" && procErr.Reason == "push notifications are disabled"
|
||||||
}
|
}
|
||||||
errStr := err.Error()
|
return false
|
||||||
return strings.Contains(errStr, "push notifications are disabled (using void processor)")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractNotificationName attempts to extract the notification name from error messages
|
// extractNotificationName attempts to extract the notification name from error messages
|
||||||
// This is a helper function for error type checking
|
|
||||||
func extractNotificationName(err error) string {
|
func extractNotificationName(err error) string {
|
||||||
// This is a simplified implementation - in practice, you might want more sophisticated parsing
|
if handlerErr, ok := err.(*HandlerError); ok {
|
||||||
// For now, we return a placeholder since the exact extraction logic depends on the error format
|
return handlerErr.PushNotificationName
|
||||||
|
}
|
||||||
|
if procErr, ok := err.(*ProcessorError); ok {
|
||||||
|
// For ProcessorError, we don't have direct access to the notification name
|
||||||
|
// but in a real implementation you could store this in the struct
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user