Skip to content

Commit

Permalink
Enhance logging related to web push
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Aug 15, 2023
1 parent 679f766 commit f1a0553
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 6 additions & 2 deletions api/resolvers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand All @@ -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} from user ${deletedSubscription.userId} due to client request`)

return deletedSubscription
}
},
Notification: {
Expand Down
5 changes: 3 additions & 2 deletions api/webPush/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} from user ${deletedSubscripton.userId} due to push error`)
} else if (err.statusCode === 413) {
console.log('[webPush] payload too large: ', err)
} else if (err.statusCode === 429) {
Expand Down

0 comments on commit f1a0553

Please sign in to comment.