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,
})
```
This commit is contained in:
Sasha
2024-10-09 21:26:58 +03:00
committed by GitHub
parent 769c94b4fd
commit 0b2a7a3606
28 changed files with 1867 additions and 450 deletions

View File

@@ -1,7 +1,10 @@
'use client'
import * as Sentry from '@sentry/react'
import React from 'react'
export const testErrors = () => {
import { useState } from 'react'
export const TestErrors = () => {
const [throwClientSide, setThrowClientSide] = useState(false)
const notFound = async () => {
const req = await fetch('http://localhost:3000/api/users/notFound', {
method: 'GET',
@@ -60,8 +63,12 @@ export const testErrors = () => {
})
}
const ThrowClientSide = () => {
throw new Error('client side error')
}
return (
<Sentry.ErrorBoundary>
<>
<h4>Test Errors</h4>
<div style={{ display: 'flex', gap: '10px' }}>
<button onClick={() => notFound()} style={{ marginBottom: '20px' }} type="button">
@@ -87,7 +94,15 @@ export const testErrors = () => {
<button onClick={() => badVerify()} style={{ marginBottom: '20px' }} type="button">
Bad Verify
</button>
<button
onClick={() => setThrowClientSide(true)}
style={{ marginBottom: '20px' }}
type="button"
>
Throw client side error
</button>
{throwClientSide && <ThrowClientSide />}
</div>
</Sentry.ErrorBoundary>
</>
)
}

View File

@@ -3,6 +3,8 @@ import path from 'path'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
import { sentryPlugin } from '@payloadcms/plugin-sentry'
import * as Sentry from '@sentry/nextjs'
import { APIError } from 'payload'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
import { devUser } from '../credentials.js'
@@ -12,7 +14,7 @@ import { Users } from './collections/Users.js'
export default buildConfigWithDefaults({
admin: {
components: {
beforeDashboard: ['/components.js#testErrors'],
beforeDashboard: ['/TestErrors.js#TestErrors'],
},
importMap: {
baseDir: path.resolve(dirname),
@@ -29,17 +31,21 @@ export default buildConfigWithDefaults({
},
})
},
endpoints: [
{
path: '/exception',
handler: () => {
throw new APIError('Test Plugin-Sentry Exception', 500)
},
method: 'get',
},
],
plugins: [
sentryPlugin({
dsn: 'https://61edebe5ee6d4d38a9d6459c7323d777@o4505289711681536.ingest.sentry.io/4505357688242176',
Sentry,
options: {
debug: true,
captureErrors: [400, 403, 404],
init: {
debug: true,
},
requestHandler: {
serverName: false,
},
},
}),
],

View File

@@ -13,6 +13,7 @@ export interface Config {
collections: {
posts: Post;
users: User;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
@@ -70,6 +71,29 @@ export interface User {
lockUntil?: string | null;
password?: string | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
id: string;
document?:
| ({
relationTo: 'posts';
value: string | Post;
} | null)
| ({
relationTo: 'users';
value: string | User;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: string | User;
};
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-preferences".