fix(webpush): improve push notification error handling

Signed-off-by: 0xsysr3ll <0xsysr3ll@pm.me>
This commit is contained in:
0xsysr3ll
2025-11-15 21:48:30 +01:00
parent 036c006aab
commit caa1716374

View File

@@ -188,19 +188,30 @@ class WebPushAgent
notificationPayload notificationPayload
); );
} catch (e) { } catch (e) {
const statusCode = (e as any).statusCode || (e as any).status;
const isPermanentFailure = statusCode === 410 || statusCode === 404;
logger.error( logger.error(
'Error sending web push notification; removing subscription', isPermanentFailure
? 'Error sending web push notification; removing invalid subscription'
: 'Error sending web push notification (transient error, keeping subscription)',
{ {
label: 'Notifications', label: 'Notifications',
recipient: pushSub.user.displayName, recipient: pushSub.user.displayName,
type: Notification[type], type: Notification[type],
subject: payload.subject, subject: payload.subject,
errorMessage: e.message, errorMessage: (e as Error).message || String(e),
statusCode: statusCode || 'unknown',
errorBody: (e as any).body || (e as any).response?.body,
endpoint: pushSub.endpoint.substring(0, 50) + '...',
} }
); );
// Failed to send notification so we need to remove the subscription // Only remove subscription for permanent failures
userPushSubRepository.remove(pushSub); // Transient errors should not remove the subscription
if (isPermanentFailure) {
await userPushSubRepository.remove(pushSub);
}
} }
}; };