Skip to content

Commit 0b2a7a3

Browse files
authored
feat(plugin-sentry): update plugin to 3.0 (#8613)
Updates the plugin to 3.0 Test: ```sh NEXT_PUBLIC_SENTRY_DSN=<DSN here> pnpm dev plugin-sentry ``` Example: ```ts sentryPlugin({ options: { captureErrors: [400, 403], context: ({ defaultContext, req }) => { return { ...defaultContext, tags: { locale: req.locale, }, } }, debug: true, }, Sentry, }) ```
1 parent 769c94b commit 0b2a7a3

28 files changed

+1867
-450
lines changed

app/global-error.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-restricted-exports */
2+
'use client'
3+
4+
import * as Sentry from '@sentry/nextjs'
5+
import NextError from 'next/error.js'
6+
import { useEffect } from 'react'
7+
8+
export default function GlobalError({ error }: { error: { digest?: string } & Error }) {
9+
useEffect(() => {
10+
if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
11+
Sentry.captureException(error)
12+
}
13+
}, [error])
14+
15+
return (
16+
<html lang="en-US">
17+
<body>
18+
{/* `NextError` is the default Next.js error page component. Its type
19+
definition requires a `statusCode` prop. However, since the App Router
20+
does not expose status codes for errors, we simply pass 0 to render a
21+
generic error message. */}
22+
{/* @ts-expect-error types repo */}
23+
<NextError statusCode={0} />
24+
</body>
25+
</html>
26+
)
27+
}

docs/plugins/sentry.mdx

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This multi-faceted software offers a range of features that will help you manage
3131
- **Integrations**: Connects with various tools and services for enhanced workflow and issue management
3232

3333
<Banner type="info">
34-
This plugin is completely open-source and the [source code can be found here](https://github.com/payloadcms/payload/tree/main/packages/plugin-sentry). If you need help, check out our [Community Help](https://payloadcms.com/community-help). If you think you've found a bug, please [open a new issue](https://github.com/payloadcms/payload/issues/new?assignees=&labels=plugin%3A%20seo&template=bug_report.md&title=plugin-seo%3A) with as much detail as possible.
34+
This plugin is completely open-source and the [source code can be found here](https://github.com/payloadcms/payload/tree/beta/packages/plugin-sentry). If you need help, check out our [Community Help](https://payloadcms.com/community-help). If you think you've found a bug, please [open a new issue](https://github.com/payloadcms/payload/issues/new?assignees=&labels=plugin%3A%20seo&template=bug_report.md&title=plugin-sentry%3A) with as much detail as possible.
3535
</Banner>
3636

3737
## Installation
@@ -42,6 +42,15 @@ Install the plugin using any JavaScript package manager like [Yarn](https://yarn
4242
pnpm add @payloadcms/plugin-sentry
4343
```
4444

45+
## Sentry for Next.js setup
46+
This plugin requires to complete the [Sentry + Next.js setup](https://docs.sentry.io/platforms/javascript/guides/nextjs/) before.
47+
48+
You can use either the [automatic setup](https://docs.sentry.io/platforms/javascript/guides/nextjs/#install) with the installation wizard:
49+
```sh
50+
npx @sentry/wizard@latest -i nextjs
51+
```
52+
Or the [Manual Setup](https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/)
53+
4554
## Basic Usage
4655

4756
In the `plugins` array of your [Payload Config](https://payloadcms.com/docs/configuration/overview), call the plugin and pass in your Sentry DSN as an option.
@@ -51,11 +60,13 @@ import { buildConfig } from 'payload'
5160
import { sentryPlugin } from '@payloadcms/plugin-sentry'
5261
import { Pages, Media } from './collections'
5362

63+
import * as Sentry from '@sentry/nextjs'
64+
5465
const config = buildConfig({
5566
collections: [Pages, Media],
5667
plugins: [
5768
sentryPlugin({
58-
dsn: 'https://[email protected]/4505357433352176',
69+
Sentry,
5970
}),
6071
],
6172
})
@@ -65,58 +76,55 @@ export default config
6576

6677
## Options
6778

68-
- `dsn` : string | **required**
79+
- `Sentry` : Sentry | **required**
6980

70-
Sentry automatically assigns a DSN when you create a project, the unique DSN informs Sentry where to send events so they are associated with the correct project.
81+
The `Sentry` instance
7182

7283
<Banner type="warning">
73-
You can find your project DSN (Data Source Name) by visiting [sentry.io](sentry.io) and navigating to your [Project] > Settings > Client Keys (DSN).
84+
Make sure to complete the [Sentry for Next.js Setup](#sentry-for-nextjs-setup) before.
7485
</Banner>
7586

7687
- `enabled`: boolean | optional
7788

78-
Set to false to disable the plugin. Defaults to true.
79-
80-
- `init` : ClientOptions | optional
81-
82-
Sentry allows a variety of options to be passed into the Sentry.init() function, see the full list of options [here](https://docs.sentry.io/platforms/node/guides/express/configuration/options).
89+
Set to false to disable the plugin. Defaults to `true`.
8390

84-
- `requestHandler` : RequestHandlerOptions | optional
91+
- `context`: `(args: ContextArgs) => Partial<ScopeContext> | Promise<Partial<ScopeContext>>`
8592

86-
Accepts options that let you decide what data should be included in the event sent to Sentry, checkout the options [here](https://docs.sentry.io/platforms/node/guides/express/configuration/options).
93+
Pass additional [contextual data](https://docs.sentry.io/platforms/javascript/enriching-events/context/#passing-context-directly) to Sentry
8794

8895
- `captureErrors`: number[] | optional
8996

9097
By default, `Sentry.errorHandler` will capture only errors with a status code of 500 or higher. To capture additional error codes, pass the values as numbers in an array.
9198

92-
To see all options available, visit the [Sentry Docs](https://docs.sentry.io/platforms/node/guides/express/configuration/options).
93-
9499
### Example
95100

96101
Configure any of these options by passing them to the plugin:
97102

98103
```ts
99104
import { buildConfig } from 'payload'
100-
import { sentry } from '@payloadcms/plugin-sentry'
105+
import { sentryPlugin } from '@payloadcms/plugin-sentry'
106+
107+
import * as Sentry from '@sentry/nextjs'
108+
101109
import { Pages, Media } from './collections'
102110

103111
const config = buildConfig({
104112
collections: [Pages, Media],
105113
plugins: [
106-
sentry({
107-
dsn: 'https://[email protected]/4505357433352176',
114+
sentryPlugin({
108115
options: {
109-
init: {
110-
debug: true,
111-
environment: 'development',
112-
tracesSampleRate: 1.0,
113-
},
114-
requestHandler: {
115-
serverName: false,
116-
user: ['email'],
116+
captureErrors: [400, 403],
117+
context: ({ defaultContext, req }) => {
118+
return {
119+
...defaultContext,
120+
tags: {
121+
locale: req.locale,
122+
},
123+
}
117124
},
118-
captureErrors: [400, 403, 404],
125+
debug: true,
119126
},
127+
Sentry,
120128
}),
121129
],
122130
})

instrumentation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function register() {
2+
if (process.env.NEXT_RUNTIME === 'nodejs') {
3+
await import('./sentry.server.config.js')
4+
}
5+
}

next.config.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import bundleAnalyzer from '@next/bundle-analyzer'
2-
3-
import withPayload from './packages/next/src/withPayload.js'
2+
import { withSentryConfig } from '@sentry/nextjs'
3+
import { withPayload } from './packages/next/src/withPayload.js'
44
import path from 'path'
55
import { fileURLToPath } from 'url'
66

@@ -11,8 +11,7 @@ const withBundleAnalyzer = bundleAnalyzer({
1111
enabled: process.env.ANALYZE === 'true',
1212
})
1313

14-
// eslint-disable-next-line no-restricted-exports
15-
export default withBundleAnalyzer(
14+
const config = withBundleAnalyzer(
1615
withPayload({
1716
eslint: {
1817
ignoreDuringBuilds: true,
@@ -23,7 +22,6 @@ export default withBundleAnalyzer(
2322
env: {
2423
PAYLOAD_CORE_DEV: 'true',
2524
ROOT_DIR: path.resolve(dirname),
26-
PAYLOAD_CI_DEPENDENCY_CHECKER: 'true',
2725
},
2826
async redirects() {
2927
return [
@@ -48,3 +46,8 @@ export default withBundleAnalyzer(
4846
},
4947
}),
5048
)
49+
50+
export default withSentryConfig(config, {
51+
telemetry: false,
52+
tunnelRoute: '/monitoring-tunnel',
53+
})

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
"@payloadcms/eslint-plugin": "workspace:*",
112112
"@payloadcms/live-preview-react": "workspace:*",
113113
"@playwright/test": "1.46.0",
114+
"@sentry/nextjs": "^8.33.1",
115+
"@sentry/node": "^8.33.1",
114116
"@swc-node/register": "1.10.9",
115117
"@swc/cli": "0.4.0",
116118
"@swc/jest": "0.2.36",

packages/plugin-form-builder/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"devDependencies": {
6161
"@payloadcms/eslint-config": "workspace:*",
6262
"@types/escape-html": "^1.0.4",
63-
"@types/express": "^4.17.21",
6463
"@types/react": "npm:[email protected]",
6564
"@types/react-dom": "npm:[email protected]",
6665
"copyfiles": "^2.4.1",

packages/plugin-redirects/package.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
},
5151
"devDependencies": {
5252
"@payloadcms/eslint-config": "workspace:*",
53-
"@types/express": "^4.17.9",
54-
"@types/react": "npm:[email protected]",
55-
"@types/react-dom": "npm:[email protected]",
5653
"payload": "workspace:*"
5754
},
5855
"peerDependencies": {
@@ -74,9 +71,5 @@
7471
"main": "./dist/index.js",
7572
"types": "./dist/index.d.ts"
7673
},
77-
"homepage:": "https://payloadcms.com",
78-
"overrides": {
79-
"@types/react": "npm:[email protected]",
80-
"@types/react-dom": "npm:[email protected]"
81-
}
74+
"homepage:": "https://payloadcms.com"
8275
}

packages/plugin-search/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
},
5656
"devDependencies": {
5757
"@payloadcms/eslint-config": "workspace:*",
58-
"@types/express": "^4.17.9",
5958
"@types/react": "npm:[email protected]",
6059
"@types/react-dom": "npm:[email protected]",
6160
"payload": "workspace:*"

packages/plugin-sentry/package.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@payloadcms/plugin-sentry",
3-
"version": "0.0.6",
3+
"version": "3.0.0-beta.111",
44
"description": "Sentry plugin for Payload",
55
"keywords": [
66
"payload",
@@ -23,6 +23,11 @@
2323
"import": "./src/index.ts",
2424
"types": "./src/index.ts",
2525
"default": "./src/index.ts"
26+
},
27+
"./client": {
28+
"import": "./src/exports/client.ts",
29+
"types": "./src/exports/client.ts",
30+
"default": "./src/exports/client.ts"
2631
}
2732
},
2833
"main": "./src/index.ts",
@@ -31,32 +36,24 @@
3136
"dist"
3237
],
3338
"scripts": {
34-
"build": "echo \"Build temporarily disabled.\" && exit 0",
39+
"build": "pnpm copyfiles && pnpm build:types && pnpm build:swc",
3540
"build:swc": "swc ./src -d ./dist --config-file .swcrc-build --strip-leading-paths",
3641
"build:types": "tsc --emitDeclarationOnly --outDir dist",
3742
"clean": "rimraf {dist,*.tsbuildinfo}",
43+
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png,json}\" dist/",
3844
"lint": "eslint .",
3945
"lint:fix": "eslint . --fix",
4046
"prepublishOnly": "pnpm clean && pnpm turbo build"
4147
},
4248
"dependencies": {
43-
"@sentry/node": "^7.55.2",
44-
"@sentry/types": "^7.54.0",
45-
"express": "^4.18.2"
49+
"@sentry/nextjs": "^8.33.1",
50+
"@sentry/types": "^8.33.1"
4651
},
4752
"devDependencies": {
4853
"@payloadcms/eslint-config": "workspace:*",
49-
"@types/express": "^4.17.9",
50-
"@types/jest": "29.5.12",
51-
"@types/node": "22.5.4",
5254
"@types/react": "npm:[email protected]",
5355
"@types/react-dom": "npm:[email protected]",
54-
"copyfiles": "^2.4.1",
55-
"cross-env": "^7.0.3",
56-
"jest": "^29.7.0",
57-
"nodemon": "3.0.3",
58-
"payload": "workspace:*",
59-
"ts-jest": "^29.1.0"
56+
"payload": "workspace:*"
6057
},
6158
"peerDependencies": {
6259
"payload": "workspace:*",
@@ -69,6 +66,11 @@
6966
"import": "./dist/index.js",
7067
"types": "./dist/index.d.ts",
7168
"default": "./dist/index.js"
69+
},
70+
"./client": {
71+
"import": "./dist/exports/client.js",
72+
"types": "./dist/exports/client.d.ts",
73+
"default": "./dist/exports/client.js"
7274
}
7375
},
7476
"main": "./dist/index.js",

packages/plugin-sentry/src/captureException.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { AdminErrorBoundary } from '../providers/AdminErrorBoundary.js'

0 commit comments

Comments
 (0)