Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase error handling

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.)

like image 533
anotherdev Avatar asked Oct 30 '22 03:10

anotherdev


1 Answers

[Developer on Cloud Functions for Firebase] Your idea is pretty clever. It will work many times but won't catch lower-level issues like the database being unavailable or a timeout in your app (though that could be fixed with a Promise.race that enqueues a retry).

We're working on adding retries to the core product. Since you've brought up the issue, I'd love to solicit some customer input. As a developer, what features do you need/expect in a retry policy? What do you feel is a sane default and how would you expect to override those defaults?

like image 104
Thomas Bouldin Avatar answered Nov 13 '22 06:11

Thomas Bouldin