|
| 1 | +--- |
| 2 | +title: "Track errors with Sentry" |
| 3 | +sidebarTitle: "Sentry error tracking" |
| 4 | +description: "This example demonstrates how to track errors with Sentry using Trigger.dev." |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Automatically send errors and source maps to your Sentry project from your Trigger.dev tasks. Sending source maps to Sentry allows for more detailed stack traces when errors occur, as Sentry can map the minified code back to the original source code. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- A [Sentry](https://sentry.io) account and project |
| 14 | +- A [Trigger.dev](https://trigger.dev) account and project |
| 15 | + |
| 16 | +## Build configuration |
| 17 | + |
| 18 | +To send errors to Sentry when there are errors in your tasks, you'll need to add this build configuration to your `trigger.config.ts` file. This will then run every time you deploy your project. |
| 19 | + |
| 20 | +<Note> |
| 21 | + You will need to set the `SENTRY_AUTH_TOKEN` and `SENTRY_DSN` environment variables. You can find |
| 22 | + the `SENTRY_AUTH_TOKEN` in your Sentry dashboard, in settings -> developer settings -> auth tokens |
| 23 | + and the `SENTRY_DSN` in your Sentry dashboard, in settings -> projects -> your project -> client |
| 24 | + keys (DSN). Add these to your `.env` file, and in your [Trigger.dev |
| 25 | + dashboard](https://cloud.trigger.dev), under environment variables in your project's sidebar. |
| 26 | +</Note> |
| 27 | + |
| 28 | +```ts trigger.config.ts |
| 29 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 30 | +import { esbuildPlugin } from "@trigger.dev/build/extensions"; |
| 31 | +import { sentryEsbuildPlugin } from "@sentry/esbuild-plugin"; |
| 32 | +import * as Sentry from "@sentry/node"; |
| 33 | + |
| 34 | +export default defineConfig({ |
| 35 | + project: "<project ref>", |
| 36 | + // Your other config settings... |
| 37 | + build: { |
| 38 | + extensions: [ |
| 39 | + esbuildPlugin( |
| 40 | + sentryEsbuildPlugin({ |
| 41 | + org: "<your-sentry-org>", |
| 42 | + project: "<your-sentry-project>", |
| 43 | + // Find this auth token in settings -> developer settings -> auth tokens |
| 44 | + authToken: process.env.SENTRY_AUTH_TOKEN, |
| 45 | + }), |
| 46 | + { placement: "last", target: "deploy" } |
| 47 | + ), |
| 48 | + ], |
| 49 | + }, |
| 50 | + init: async () => { |
| 51 | + Sentry.init({ |
| 52 | + // The Data Source Name (DSN) is a unique identifier for your Sentry project. |
| 53 | + dsn: process.env.SENTRY_DSN, |
| 54 | + // Update this to match the environment you want to track errors for |
| 55 | + environment: process.env.NODE_ENV === "production" ? "production" : "development", |
| 56 | + }); |
| 57 | + }, |
| 58 | + onFailure: async (payload, error, { ctx }) => { |
| 59 | + Sentry.captureException(error, { |
| 60 | + extra: { |
| 61 | + payload, |
| 62 | + ctx, |
| 63 | + }, |
| 64 | + }); |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +<Note> |
| 70 | + [Build extensions](/config/config-file#extensions) allow you to hook into the build system and |
| 71 | + customize the build process or the resulting bundle and container image (in the case of |
| 72 | + deploying). You can use pre-built extensions or create your own. |
| 73 | +</Note> |
| 74 | + |
| 75 | +## Testing that errors are being sent to Sentry |
| 76 | + |
| 77 | +To test that errors are being sent to Sentry, you need to create a task that will fail. |
| 78 | + |
| 79 | +This task takes no payload, and will throw an error. |
| 80 | + |
| 81 | +```ts trigger/sentry-error-test.ts |
| 82 | +import { task } from "@trigger.dev/sdk/v3"; |
| 83 | + |
| 84 | +export const sentryErrorTest = task({ |
| 85 | + id: "sentry-error-test", |
| 86 | + retry: { |
| 87 | + // Only retry once |
| 88 | + maxAttempts: 1, |
| 89 | + }, |
| 90 | + run: async () => { |
| 91 | + const error = new Error("This is a custom error that Sentry will capture"); |
| 92 | + error.cause = { additionalContext: "This is additional context" }; |
| 93 | + throw error; |
| 94 | + }, |
| 95 | +}); |
| 96 | +``` |
| 97 | + |
| 98 | +After creating the task, deploy your project. |
| 99 | + |
| 100 | + <CodeGroup> |
| 101 | + |
| 102 | + ```bash npm |
| 103 | + npx trigger.dev@latest deploy |
| 104 | + ``` |
| 105 | + |
| 106 | + ```bash pnpm |
| 107 | + pnpm dlx trigger.dev@latest deploy |
| 108 | + ``` |
| 109 | + |
| 110 | + ```bash yarn |
| 111 | + yarn dlx trigger.dev@latest deploy |
| 112 | + ``` |
| 113 | + |
| 114 | + </CodeGroup> |
| 115 | + |
| 116 | +Once deployed, navigate to the `test` page in the sidebar of your [Trigger.dev dashboard](https://cloud.trigger.dev), click on your `prod` environment, and select the `sentryErrorTest` task. |
| 117 | + |
| 118 | +Run a test task with an empty payload by clicking the `Run test` button. |
| 119 | + |
| 120 | +Your run should then fail, and if everything is set up correctly, you will see an error in the Sentry project dashboard shortly after. |
0 commit comments