Skip to content

Commit 2aeed5f

Browse files
committed
refactor: update WithDroppedNotification to use getter/setter for OnDroppedNotification
1 parent b1cc37a commit 2aeed5f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

ro.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func SetOnUnhandledError(fn func(ctx context.Context, err error)) {
4646

4747
// GetOnUnhandledError returns the currently configured unhandled-error handler.
4848
func GetOnUnhandledError() func(ctx context.Context, err error) {
49-
return onUnhandledError.Load().(func(context.Context, error))
49+
v := onUnhandledError.Load()
50+
if fn, ok := v.(func(context.Context, error)); ok && fn != nil {
51+
return fn
52+
}
53+
return IgnoreOnUnhandledError
5054
}
5155

5256
// OnUnhandledError calls the currently configured unhandled-error handler.
@@ -65,7 +69,11 @@ func SetOnDroppedNotification(fn func(ctx context.Context, notification fmt.Stri
6569

6670
// GetOnDroppedNotification returns the currently configured dropped-notification handler.
6771
func GetOnDroppedNotification() func(ctx context.Context, notification fmt.Stringer) {
68-
return onDroppedNotification.Load().(func(context.Context, fmt.Stringer))
72+
v := onDroppedNotification.Load()
73+
if fn, ok := v.(func(context.Context, fmt.Stringer)); ok && fn != nil {
74+
return fn
75+
}
76+
return IgnoreOnDroppedNotification
6977
}
7078

7179
// OnDroppedNotification calls the currently configured dropped-notification handler.

subscriber_test_helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func WithDroppedNotification(t *testing.T, handler func(ctx context.Context, not
3535
t.Helper()
3636

3737
droppedNotificationMu.Lock()
38-
prev := OnDroppedNotification
39-
OnDroppedNotification = handler
38+
prev := GetOnDroppedNotification()
39+
SetOnDroppedNotification(handler)
4040

4141
// Ensure restore and unlock even if fn panics.
4242
defer func() {
43-
OnDroppedNotification = prev
43+
SetOnDroppedNotification(prev)
4444
droppedNotificationMu.Unlock()
4545
}()
4646

0 commit comments

Comments
 (0)