|
| 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 to your Sentry project from your Trigger.dev tasks. |
| 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 | + Make sure you use the correct sentry package for your runtime. For example, if you are using |
| 22 | + Node.js, you should use the `@sentry/node` package. For a full list of supported packages, see the |
| 23 | + [Sentry docs](https://docs.sentry.io/platforms/). |
| 24 | +</Note> |
| 25 | + |
| 26 | +<Note> |
| 27 | + You will need to set the `SENTRY_AUTH_TOKEN` and `SENTRY_DSN` environment variables. You can find |
| 28 | + the `SENTRY_AUTH_TOKEN` in your Sentry dashboard, in settings -> developer settings -> auth tokens |
| 29 | + and the `SENTRY_DSN` in your Sentry dashboard, in settings -> projects -> your project -> client |
| 30 | + keys (DSN). Add these to your `.env` file, and in your [Trigger.dev |
| 31 | + dashboard](https://cloud.trigger.dev), under environment variables in your project's sidebar. |
| 32 | +</Note> |
| 33 | + |
| 34 | +```ts trigger.config.ts |
| 35 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 36 | +import { sentryEsbuildPlugin } from "@sentry/esbuild-plugin"; |
| 37 | +// Import the correct Sentry package for your runtime, e.g. @sentry/node for Node.js or @Sentry/nextjs for Next.js |
| 38 | +import * as Sentry from "@sentry/node"; |
| 39 | + |
| 40 | +export default defineConfig({ |
| 41 | + project: "<project ref>", |
| 42 | + // Your other config settings... |
| 43 | + build: { |
| 44 | + extensions: [ |
| 45 | + additionalPackages({ |
| 46 | + // Add the correct Sentry package for your runtime, e.g. @sentry/node for Node.js or @Sentry/nextjs for Next.js |
| 47 | + packages: ["@sentry/node"], |
| 48 | + }), |
| 49 | + esbuildPlugin( |
| 50 | + sentryEsbuildPlugin({ |
| 51 | + org: "<your-sentry-org>", |
| 52 | + project: "<your-sentry-project>", |
| 53 | + // Find this auth token in settings -> developer settings -> auth tokens |
| 54 | + authToken: process.env.SENTRY_AUTH_TOKEN, |
| 55 | + }), |
| 56 | + // Optional - only runs during the deploy command, and adds the plugin to the end of the list of plugins |
| 57 | + { placement: "last", target: "deploy" } |
| 58 | + ), |
| 59 | + ], |
| 60 | + }, |
| 61 | + init: async () => { |
| 62 | + Sentry.init({ |
| 63 | + // The Data Source Name (DSN) is a unique identifier for your Sentry project. |
| 64 | + |
| 65 | + dsn: process.env.SENTRY_DSN, |
| 66 | + // Update this to match the environment you want to track errors for |
| 67 | + environment: process.env.NODE_ENV === "production" ? "production" : "development", |
| 68 | + }); |
| 69 | + }, |
| 70 | + onFailure: async (payload, error, { ctx }) => { |
| 71 | + Sentry.captureException(error, { |
| 72 | + extra: { |
| 73 | + payload, |
| 74 | + ctx, |
| 75 | + }, |
| 76 | + }); |
| 77 | + }, |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +<Note> |
| 82 | + [Build extensions](/config/config-file#extensions) allow you to hook into the build system and |
| 83 | + customize the build process or the resulting bundle and container image (in the case of |
| 84 | + deploying). You can use pre-built extensions or create your own. |
| 85 | +</Note> |
| 86 | + |
| 87 | +## Testing that errors are being sent to Sentry |
| 88 | + |
| 89 | +To test that errors are being sent to Sentry, you need to create a task that will fail. |
| 90 | + |
| 91 | +This task takes no payload, and will throw an error. |
| 92 | + |
| 93 | +```ts trigger/sentry-error-test.ts |
| 94 | +import { task } from "@trigger.dev/sdk/v3"; |
| 95 | + |
| 96 | +export const sentryErrorTest = task({ |
| 97 | + id: "sentry-error-test", |
| 98 | + retry: { |
| 99 | + // Only retry once |
| 100 | + maxAttempts: 1, |
| 101 | + }, |
| 102 | + run: async () => { |
| 103 | + const error = new Error("This is a custom error that Sentry will capture"); |
| 104 | + error.cause = { additionalContext: "This is additional context" }; |
| 105 | + throw error; |
| 106 | + }, |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +After creating the task, deploy your project. |
| 111 | + |
| 112 | + <CodeGroup> |
| 113 | + |
| 114 | + ```bash npm |
| 115 | + npx trigger.dev@latest deploy |
| 116 | + ``` |
| 117 | + |
| 118 | + ```bash pnpm |
| 119 | + pnpm dlx trigger.dev@latest deploy |
| 120 | + ``` |
| 121 | + |
| 122 | + ```bash yarn |
| 123 | + yarn dlx trigger.dev@latest deploy |
| 124 | + ``` |
| 125 | + |
| 126 | + </CodeGroup> |
| 127 | + |
| 128 | +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. |
| 129 | + |
| 130 | +Run a test task with an empty payload by clicking the `Run test` button. |
| 131 | + |
| 132 | +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