Skip to content

Commit 90856b1

Browse files
committed
configure sentry for runtime error logging
1 parent 187cf94 commit 90856b1

10 files changed

+520
-13
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ yarn-debug.log*
2525
yarn-error.log*
2626

2727
# local env files
28+
.env
2829
.env.local
2930
.env.development.local
3031
.env.test.local
@@ -40,4 +41,9 @@ yarn-error.log*
4041
.vscode
4142

4243
# relay
43-
__generated__
44+
__generated__
45+
# Sentry
46+
.sentryclirc
47+
48+
# Sentry
49+
next.config.original.js

next.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// This file sets a custom webpack configuration to use your Next.js app
2+
// with Sentry.
3+
// https://nextjs.org/docs/api-reference/next.config.js/introduction
4+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
5+
import { withSentryConfig } from '@sentry/nextjs'
6+
17
/** @type {import('next').NextConfig} */
28
const nextConfig = {
39
// webpack: (config, { isServer, webpack }) => {
@@ -32,3 +38,9 @@ const nextConfig = {
3238
}
3339

3440
module.exports = nextConfig
41+
42+
module.exports = withSentryConfig(
43+
module.exports,
44+
{ silent: true },
45+
{ hideSourcemaps: true }
46+
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@editorjs/table": "^2.0.2",
2626
"@firstcontributions/editorjs-codeflask": "https://github.com/firstcontributions/editorjs-codeflask",
2727
"@react-icons/all-files": "^4.1.0",
28+
"@sentry/nextjs": "^7.37.0",
2829
"dayjs": "^1.11.5",
2930
"form-data": "^4.0.0",
3031
"formidable": "^2.0.1",

sentry.client.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the browser.
2+
// The config you add here will be used whenever a page is visited.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN,
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
})

sentry.edge.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever middleware or an Edge route handles a request.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN,
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
})

sentry.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defaults.url=https://sentry.io/
2+
defaults.org=opensourcer
3+
defaults.project=frontend
4+
cli.executable=node_modules/@sentry/cli/bin/sentry-cli

sentry.server.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever the server handles a request.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN,
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
})

src/pages/_error.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher.
3+
*
4+
* NOTE: If using this with `next` version 12.2.0 or lower, uncomment the
5+
* penultimate line in `CustomErrorComponent`.
6+
*
7+
* This page is loaded by Nextjs:
8+
* - on the server, when data-fetching methods throw or reject
9+
* - on the client, when `getInitialProps` throws or rejects
10+
* - on the client, when a React lifecycle method throws or rejects, and it's
11+
* caught by the built-in Nextjs error boundary
12+
*
13+
* See:
14+
* - https://nextjs.org/docs/basic-features/data-fetching/overview
15+
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
16+
* - https://reactjs.org/docs/error-boundaries.html
17+
*/
18+
19+
import * as Sentry from '@sentry/nextjs'
20+
import NextErrorComponent from 'next/error'
21+
22+
const CustomErrorComponent = (props) => {
23+
// If you're using a Nextjs version prior to 12.2.1, uncomment this to
24+
// compensate for https://github.com/vercel/next.js/issues/8592
25+
// Sentry.captureUnderscoreErrorException(props);
26+
27+
return <NextErrorComponent statusCode={props.statusCode} />
28+
}
29+
30+
CustomErrorComponent.getInitialProps = async (contextData) => {
31+
// In case this is running in a serverless function, await this in order to give Sentry
32+
// time to send the error before the lambda exits
33+
await Sentry.captureUnderscoreErrorException(contextData)
34+
35+
// This will contain the status code of the response
36+
return NextErrorComponent.getInitialProps(contextData)
37+
}
38+
39+
export default CustomErrorComponent

src/pages/sentry_sample_error.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Head from 'next/head'
2+
3+
const boxStyles = {
4+
padding: '12px',
5+
border: '1px solid #eaeaea',
6+
borderRadius: '10px',
7+
}
8+
export default function Home() {
9+
return (
10+
<div>
11+
<Head>
12+
<title>Sentry Onboarding</title>
13+
<meta name="description" content="Make your Next.js ready for Sentry" />
14+
</Head>
15+
16+
<main
17+
style={{
18+
minHeight: '100vh',
19+
display: 'flex',
20+
flexDirection: 'column',
21+
justifyContent: 'center',
22+
alignItems: 'center',
23+
}}
24+
>
25+
<h1 style={{ fontSize: '4rem' }}>
26+
<svg
27+
style={{
28+
height: '1em',
29+
}}
30+
xmlns="http://www.w3.org/2000/svg"
31+
viewBox="0 0 200 44"
32+
>
33+
<path
34+
fill="currentColor"
35+
d="M124.32,28.28,109.56,9.22h-3.68V34.77h3.73V15.19l15.18,19.58h3.26V9.22h-3.73ZM87.15,23.54h13.23V20.22H87.14V12.53h14.93V9.21H83.34V34.77h18.92V31.45H87.14ZM71.59,20.3h0C66.44,19.06,65,18.08,65,15.7c0-2.14,1.89-3.59,4.71-3.59a12.06,12.06,0,0,1,7.07,2.55l2-2.83a14.1,14.1,0,0,0-9-3c-5.06,0-8.59,3-8.59,7.27,0,4.6,3,6.19,8.46,7.52C74.51,24.74,76,25.78,76,28.11s-2,3.77-5.09,3.77a12.34,12.34,0,0,1-8.3-3.26l-2.25,2.69a15.94,15.94,0,0,0,10.42,3.85c5.48,0,9-2.95,9-7.51C79.75,23.79,77.47,21.72,71.59,20.3ZM195.7,9.22l-7.69,12-7.64-12h-4.46L186,24.67V34.78h3.84V24.55L200,9.22Zm-64.63,3.46h8.37v22.1h3.84V12.68h8.37V9.22H131.08ZM169.41,24.8c3.86-1.07,6-3.77,6-7.63,0-4.91-3.59-8-9.38-8H154.67V34.76h3.8V25.58h6.45l6.48,9.2h4.44l-7-9.82Zm-10.95-2.5V12.6h7.17c3.74,0,5.88,1.77,5.88,4.84s-2.29,4.86-5.84,4.86Z M29,2.26a4.67,4.67,0,0,0-8,0L14.42,13.53A32.21,32.21,0,0,1,32.17,40.19H27.55A27.68,27.68,0,0,0,12.09,17.47L6,28a15.92,15.92,0,0,1,9.23,12.17H4.62A.76.76,0,0,1,4,39.06l2.94-5a10.74,10.74,0,0,0-3.36-1.9l-2.91,5a4.54,4.54,0,0,0,1.69,6.24A4.66,4.66,0,0,0,4.62,44H19.15a19.4,19.4,0,0,0-8-17.31l2.31-4A23.87,23.87,0,0,1,23.76,44H36.07a35.88,35.88,0,0,0-16.41-31.8l4.67-8a.77.77,0,0,1,1.05-.27c.53.29,20.29,34.77,20.66,35.17a.76.76,0,0,1-.68,1.13H40.6q.09,1.91,0,3.81h4.78A4.59,4.59,0,0,0,50,39.43a4.49,4.49,0,0,0-.62-2.28Z"
36+
></path>
37+
</svg>
38+
</h1>
39+
40+
<p>Get started by sending us a sample error</p>
41+
<button
42+
type="button"
43+
style={{
44+
...boxStyles,
45+
backgroundColor: '#c73852',
46+
borderRadius: '12px',
47+
border: 'none',
48+
}}
49+
onClick={() => {
50+
throw new Error('Sentry Frontend Error')
51+
}}
52+
>
53+
Throw error
54+
</button>
55+
56+
<p>
57+
For more information, see{' '}
58+
<a href="https://docs.sentry.io/platforms/javascript/guides/nextjs/">
59+
https://docs.sentry.io/platforms/javascript/guides/nextjs/
60+
</a>
61+
</p>
62+
</main>
63+
</div>
64+
)
65+
}

0 commit comments

Comments
 (0)