Last active
July 21, 2021 15:30
-
-
Save lukaszsagol/f65db5e77be2bad9684b65d1625a193f to your computer and use it in GitHub Desktop.
How to capture TypeScript Firebase functions errors in Sentry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code for tutorial How to capture TypeScript Firebase functions errors in Sentry