Enhance logging related to web push (#412)

Co-authored-by: ekzyis <ek@stacker.news>
This commit is contained in:
ekzyis 2023-08-15 19:59:58 +02:00 committed by GitHub
parent e6ee7f73c4
commit 7c426aa420
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -238,10 +238,12 @@ export default {
dbPushSubscription = await models.pushSubscription.update({
data: { userId: me.id, endpoint, p256dh, auth }, where: { endpoint: oldEndpoint }
})
console.log(`[webPush] updated subscription of user ${me.id}: old=${oldEndpoint} new=${endpoint}`)
} else {
dbPushSubscription = await models.pushSubscription.create({
data: { userId: me.id, endpoint, p256dh, auth }
})
console.log(`[webPush] created subscription for user ${me.id}: endpoint=${endpoint}`)
}
await replyToSubscription(dbPushSubscription.id, { title: 'Stacker News notifications are now active' })
@ -257,8 +259,10 @@ export default {
if (!subscription) {
throw new GraphQLError('endpoint not found', { extensions: { code: 'BAD_INPUT' } })
}
await models.pushSubscription.delete({ where: { id: subscription.id } })
return subscription
const deletedSubscription = await models.pushSubscription.delete({ where: { id: subscription.id } })
console.log(`[webPush] deleted subscription ${deletedSubscription.id} of user ${deletedSubscription.userId} due to client request`)
return deletedSubscription
}
},
Notification: {

View File

@ -57,14 +57,15 @@ const sendNotification = (subscription, payload) => {
}
const { id, endpoint, p256dh, auth } = subscription
return webPush.sendNotification({ endpoint, keys: { p256dh, auth } }, payload)
.catch((err) => {
.catch(async (err) => {
if (err.statusCode === 400) {
console.log('[webPush] invalid request: ', err)
} else if ([401, 403].includes(err.statusCode)) {
console.log('[webPush] auth error: ', err)
} else if (err.statusCode === 404 || err.statusCode === 410) {
console.log('[webPush] subscription has expired or is no longer valid: ', err)
return models.pushSubscription.delete({ where: { id } })
const deletedSubscripton = await models.pushSubscription.delete({ where: { id } })
console.log(`[webPush] deleted subscription ${id} of user ${deletedSubscripton.userId} due to push error`)
} else if (err.statusCode === 413) {
console.log('[webPush] payload too large: ', err)
} else if (err.statusCode === 429) {