How to capture TypeScript Firebase functions errors in Sentry

  • Łukasz Sągol
    Łukasz Sągol
    Co-founder
We’re building a whiteboard for software engineers. Read about how

If you use TypeScript, Cloud Functions for Firebase and Sentry, there’s an easy way to ensure that any error that occurs in a function is automatically sent to Sentry.

If you write your Firebase functions in a different language, you can almost certainly adapt the code sample below to achieve the same objectives, but for the sake of convenience we’re assuming that you’re writing in TypeScript and using the @sentry/node library to access Sentry.

Using wrappers to catch errors

We’ve created a set of Sentry ‘wrappers’ for Firebase functions that give you a one-line way to capture errors from:

Each wrapper follows the same pattern and sequence of events:

  1. Start a Sentry transaction
  2. Set the transaction context to provide some more information about the function that’s being tracked – e.g. the function name
  3. Try calling the function handler itself
  4. Catch any errors and send them to Sentry
  5. Finish the Sentry transaction

The Sentry wrapper

We’re using onCall functions as an example here but the same principles apply to other types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as Sentry from '@sentry/node'
import { https } from 'firebase-functions'

Sentry.init(
  // Add your Sentry configuration here or import it
)

export const httpsOnCallWrapper = (

  // We pass an identifying ‘name’ as a string
  // This will show up in our Sentry error titles
  // so it needs to a) be unique and b) make sense

  name: string,

  // This is the handler itself, which previously
  // you would have exported directly from the
  // function file

  handler: (data: any, context: https.CallableContext) =>
    any | Promise<any>

) => {
  return async (data: any, context: https.CallableContext) => {

    // 1. Start the Sentry transaction

    const transaction = Sentry.startTransaction({
      name,
      op: 'functions.https.onCall',
    })

    // 2. Set the transaction context
    // In this example, we’re sending the uid from Firebase auth
    // You can send any relevant data here that might help with
    // debugging

    Sentry.setContext('Function context', {
      ...(data || {}),
      uid: context.auth?.uid,
      function: name,
      op: 'functions.https.onCall',
    })

    try {

      // 3. Try calling the function handler itself

      return await handler(data, context)

    } catch (e) {

      // 4. Send any errors to Sentry

      await Sentry.captureException(e)
      await Sentry.flush(1000)

      // Don’t forget to throw them too!

      throw e

    } finally {

      // 5. Finish the Sentry transaction

      Sentry.configureScope((scope) => scope.clear())
      transaction.finish()

    }
  }
}

Suggestions for wrapping other function types:

  • You’ll need to change the argument types for the wrapper and its return to match the expected arguments of the function type
  • For triggered functions, we find it useful to capture some additional information about the trigger in the Sentry context. For example, in Firestore triggered functions, we capture the path of the Firestore document that activated the trigger.

Wrapping functions

To put it all together, take a look at this simple function example:

1
2
3
4
5
6
7
8
9
import * as functions from 'firebase-functions'

const helloWorldHandler = async () => {
  // Do exciting function things here

  return { done: true }
}

exports = module.exports = functions.https.onCall(helloWorldHandler)

Now, to wrap it, all you need to do is:

1
2
3
exports = module.exports = functions.https.onCall(
  httpsOnCallWrapper('helloWorld', helloWorldHandler)
)

instead, and any errors in the helloWorldHandler() function will be captured in Sentry.

If you have questions or comments about this post, please tweet us @qualdesk or me @lukaszsagol. And let us know how you get on.