Skip to content

Commit 11ecbaf

Browse files
committed
fix(push): fix tests
1 parent 84f788e commit 11ecbaf

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

push/errors.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,47 @@ import (
88
// Push notification error definitions
99
// This file contains all error types and messages used by the push notification system
1010

11+
// Error reason constants
12+
const (
13+
// HandlerReasons
14+
ReasonHandlerNil = "handler cannot be nil"
15+
ReasonHandlerExists = "cannot overwrite existing handler"
16+
ReasonHandlerProtected = "handler is protected"
17+
18+
// ProcessorReasons
19+
ReasonPushNotificationsDisabled = "push notifications are disabled"
20+
)
21+
1122
// Common error variables for reuse
1223
var (
1324
// ErrHandlerNil is returned when attempting to register a nil handler
14-
ErrHandlerNil = errors.New("handler cannot be nil")
25+
ErrHandlerNil = errors.New(ReasonHandlerNil)
1526
)
1627

1728
// Registry errors
1829

1930
// ErrHandlerExists creates an error for when attempting to overwrite an existing handler
2031
func ErrHandlerExists(pushNotificationName string) error {
21-
return NewHandlerError("register", pushNotificationName, "cannot overwrite existing handler", nil)
32+
return NewHandlerError("register", pushNotificationName, ReasonHandlerExists, nil)
2233
}
2334

2435
// ErrProtectedHandler creates an error for when attempting to unregister a protected handler
2536
func ErrProtectedHandler(pushNotificationName string) error {
26-
return NewHandlerError("unregister", pushNotificationName, "handler is protected", nil)
37+
return NewHandlerError("unregister", pushNotificationName, ReasonHandlerProtected, nil)
2738
}
2839

2940
// VoidProcessor errors
3041

3142
// ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor
3243
func ErrVoidProcessorRegister(pushNotificationName string) error {
33-
return NewProcessorError("void_processor", "register", pushNotificationName, "push notifications are disabled", nil)
44+
return NewProcessorError("void_processor", "register", pushNotificationName, ReasonPushNotificationsDisabled, nil)
3445
}
3546

3647
// ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor
3748
func ErrVoidProcessorUnregister(pushNotificationName string) error {
38-
return NewProcessorError("void_processor", "unregister", pushNotificationName, "push notifications are disabled", nil)
49+
return NewProcessorError("void_processor", "unregister", pushNotificationName, ReasonPushNotificationsDisabled, nil)
3950
}
4051

41-
// Error message constants for consistency
42-
const (
43-
// Error message templates
44-
MsgHandlerNil = "handler cannot be nil"
45-
MsgHandlerExists = "cannot overwrite existing handler for push notification: %s"
46-
MsgProtectedHandler = "cannot unregister protected handler for push notification: %s"
47-
MsgVoidProcessorRegister = "cannot register push notification handler '%s': push notifications are disabled (using void processor)"
48-
MsgVoidProcessorUnregister = "cannot unregister push notification handler '%s': push notifications are disabled (using void processor)"
49-
)
50-
5152
// Error type definitions for advanced error handling
5253

5354
// HandlerError represents errors related to handler operations
@@ -124,23 +125,23 @@ func IsHandlerNilError(err error) bool {
124125
// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler
125126
func IsHandlerExistsError(err error) bool {
126127
if handlerErr, ok := err.(*HandlerError); ok {
127-
return handlerErr.Operation == "register" && handlerErr.Reason == "cannot overwrite existing handler"
128+
return handlerErr.Operation == "register" && handlerErr.Reason == ReasonHandlerExists
128129
}
129130
return false
130131
}
131132

132133
// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler
133134
func IsProtectedHandlerError(err error) bool {
134135
if handlerErr, ok := err.(*HandlerError); ok {
135-
return handlerErr.Operation == "unregister" && handlerErr.Reason == "handler is protected"
136+
return handlerErr.Operation == "unregister" && handlerErr.Reason == ReasonHandlerProtected
136137
}
137138
return false
138139
}
139140

140141
// IsVoidProcessorError checks if an error is due to void processor operations
141142
func IsVoidProcessorError(err error) bool {
142143
if procErr, ok := err.(*ProcessorError); ok {
143-
return procErr.ProcessorType == "void_processor" && procErr.Reason == "push notifications are disabled"
144+
return procErr.ProcessorType == "void_processor" && procErr.Reason == ReasonPushNotificationsDisabled
144145
}
145146
return false
146147
}

push/push_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,27 +1653,23 @@ func TestErrorHelperFunctions(t *testing.T) {
16531653
})
16541654
}
16551655

1656-
// TestErrorConstants tests the error message constants
1656+
// TestErrorConstants tests the error reason constants
16571657
func TestErrorConstants(t *testing.T) {
1658-
t.Run("ErrorMessageConstants", func(t *testing.T) {
1659-
if MsgHandlerNil != "handler cannot be nil" {
1660-
t.Errorf("MsgHandlerNil should be 'handler cannot be nil', got: %s", MsgHandlerNil)
1658+
t.Run("ErrorReasonConstants", func(t *testing.T) {
1659+
if ReasonHandlerNil != "handler cannot be nil" {
1660+
t.Errorf("ReasonHandlerNil should be 'handler cannot be nil', got: %s", ReasonHandlerNil)
16611661
}
16621662

1663-
if MsgHandlerExists != "cannot overwrite existing handler for push notification: %s" {
1664-
t.Errorf("MsgHandlerExists should be 'cannot overwrite existing handler for push notification: %%s', got: %s", MsgHandlerExists)
1663+
if ReasonHandlerExists != "cannot overwrite existing handler" {
1664+
t.Errorf("ReasonHandlerExists should be 'cannot overwrite existing handler', got: %s", ReasonHandlerExists)
16651665
}
16661666

1667-
if MsgProtectedHandler != "cannot unregister protected handler for push notification: %s" {
1668-
t.Errorf("MsgProtectedHandler should be 'cannot unregister protected handler for push notification: %%s', got: %s", MsgProtectedHandler)
1667+
if ReasonHandlerProtected != "handler is protected" {
1668+
t.Errorf("ReasonHandlerProtected should be 'handler is protected', got: %s", ReasonHandlerProtected)
16691669
}
16701670

1671-
if MsgVoidProcessorRegister != "cannot register push notification handler '%s': push notifications are disabled (using void processor)" {
1672-
t.Errorf("MsgVoidProcessorRegister constant mismatch, got: %s", MsgVoidProcessorRegister)
1673-
}
1674-
1675-
if MsgVoidProcessorUnregister != "cannot unregister push notification handler '%s': push notifications are disabled (using void processor)" {
1676-
t.Errorf("MsgVoidProcessorUnregister constant mismatch, got: %s", MsgVoidProcessorUnregister)
1671+
if ReasonPushNotificationsDisabled != "push notifications are disabled" {
1672+
t.Errorf("ReasonPushNotificationsDisabled should be 'push notifications are disabled', got: %s", ReasonPushNotificationsDisabled)
16771673
}
16781674
})
16791675
}

0 commit comments

Comments
 (0)