164
* @param fn - The function to call when the event is received
165
* @param onSubscriptionError - Optional callback invoked with the raw payload when the
166
* Pusher subscription fails (for example on auth or permission rejection). Errors thrown
167
- * from this callback are contained and logged, never rethrown.
167
+ * from — or promises rejected by — this callback are contained and logged, never rethrown.
168
*/
169
async subscribe(
170
fn: (data: Record<string, unknown>) => void,
176
const channel = await pusherClient.subscribe(this.pusherChannel);
177
178
// add subscription error handling
179
+ const logCallbackFailure = (callbackError: unknown) => {
180
+ const errorMessage =
181
+ callbackError instanceof Error ? callbackError.message : String(callbackError);
182
+ logger.error('❌ Error in subscription error callback:', errorMessage);
183
+ };
184
channel.bind('pusher:subscription_error', (data: Record<string, unknown>) => {
185
logger.error('Trigger subscription error:', data);
186
187
// surface the failure to the caller without letting a faulty
183
- // handler crash the host (same containment as the trigger callback)
188
+ // handler crash the host (same containment as the trigger callback).
189
+ // A handler may be async: contain rejected promises too, or the
190
+ // rejection escapes as an unhandled rejection after subscribe()
191
+ // already resolved.
192
try {
185
- onSubscriptionError?.(data);
193
+ Promise.resolve(onSubscriptionError?.(data)).catch(logCallbackFailure);
194
} catch (callbackError: unknown) {
187
- const errorMessage =
188
- callbackError instanceof Error ? callbackError.message : String(callbackError);
189
- logger.error('❌ Error in subscription error callback:', errorMessage);
195
+ logCallbackFailure(callbackError);
196
}
197
});
198