How to capture TypeScript Firebase Functions errors in Sentry

Łukasz Sągol
Qualdesk
Published in
2 min readJul 19, 2021

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.

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:

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

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.

Try Qualdesk with your team for free

We’re building a data-powered team whiteboard. No more copying, pasting and tidying up after meetings. Qualdesk instantly updates your team’s tools, while you work.

Make your next meeting better by running it in Qualdesk: https://qualdesk.com/

Originally published at https://www.qualdesk.com.

--

--