I'm wondering what's the right way to write the node.js code when I have chained promises and I need to update the realtime database if something goes wrong?
Here is the code:
export const testErrorHandling = functions.database
.ref('/workqueue/{pushId}/something').onWrite(event => {
// Exit when the data is deleted.
if (!event.data.exists()) {
return;
}
//This is the retry count, give up if more than 5 times have been retried.
const data = event.data.val()
if (data.count >= 5) {
return
}
return event.data.ref.root.child(data.fulluri).once('value').then(snapshot => {
//Process all, if ok, delete the work queue entry
return event.data.ref.remove()
}).catch(exception => {
console.log('Error!: ' + exception)
//Log error, increase retry count by one an write to that
//location to trigger a retry
//Is the line below OK?
//return event.data.ref.child('count').set(data.count + 1)
})
})
I would guess this is common requirement in many cases, but couldn't find an example as all examples seem just to write to console.error and be done. (Which in real world is seldom enough.)