Skip to content

Commit

Permalink
feat: add sentry to /api/bookmarks route
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Jun 13, 2022
1 parent 9346e2a commit bb689c9
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ yarn-error.log*
# vercel
.vercel
log.txt

# Sentry
.sentryclirc
16 changes: 15 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { withSentryConfig } = require('@sentry/nextjs')

/**
* @type {import('next').NextConfig}
*/
Expand All @@ -22,4 +24,16 @@ const nextConfig = {
},
}

export default nextConfig
const sentryWebpackPluginOptions = {
// Additional config options for the Sentry Webpack plugin. Keep in mind that
// the following options are set automatically, and overriding them is not
// recommended:
// release, url, org, project, authToken, configFile, stripPrefix,
// urlPrefix, include, ignore

silent: true, // Suppresses all logs
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options.
}

export default withSentryConfig(nextConfig, sentryWebpackPluginOptions)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@headlessui/react": "^1.6.4",
"@next-auth/prisma-adapter": "^1.0.3",
"@prisma/client": "^3.15.1",
"@sentry/nextjs": "^7.1.1",
"chrome-aws-lambda": "^9.1.0",
"eslint-plugin-prettier": "^4.0.0",
"framer-motion": "^6.3.11",
Expand Down
305 changes: 212 additions & 93 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions sentry.client.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This file configures the initialization of Sentry on the browser.
// The config you add here will be used whenever a page is visited.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs'

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN

Sentry.init({
dsn:
SENTRY_DSN ||
'https://2b0f5bb15ddd4feeb9fbed676243f0b6@o1287700.ingest.sentry.io/6503207',
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1.0,
// ...
// Note: if you want to override the automatic release value, do not set a
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
// that it will also get attached to your source maps
})
4 changes: 4 additions & 0 deletions sentry.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defaults.url=https://sentry.io/
defaults.org=domino-z6
defaults.project=briefkasten
cli.executable=../../../home/ndo/.npm/_npx/a8388072043b4cbc/node_modules/@sentry/cli/bin/sentry-cli
19 changes: 19 additions & 0 deletions sentry.server.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This file configures the initialization of Sentry on the server.
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs'

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN

Sentry.init({
dsn:
SENTRY_DSN ||
'https://2b0f5bb15ddd4feeb9fbed676243f0b6@o1287700.ingest.sentry.io/6503207',
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1.0,
// ...
// Note: if you want to override the automatic release value, do not set a
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
// that it will also get attached to your source maps
})
65 changes: 65 additions & 0 deletions src/pages/_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import NextErrorComponent from 'next/error'

import * as Sentry from '@sentry/nextjs'

const MyError = ({ statusCode, hasGetInitialPropsRun, err }) => {
if (!hasGetInitialPropsRun && err) {
// getInitialProps is not called in case of
// https://github.com/vercel/next.js/issues/8592. As a workaround, we pass
// err via _app.js so it can be captured
Sentry.captureException(err)
// Flushing is not required in this case as it only happens on the client
}

return <NextErrorComponent statusCode={statusCode} />
}

MyError.getInitialProps = async (context) => {
const errorInitialProps = await NextErrorComponent.getInitialProps(context)

const { res, err, asPath } = context

// Workaround for https://github.com/vercel/next.js/issues/8592, mark when
// getInitialProps has run
errorInitialProps.hasGetInitialPropsRun = true

// Returning early because we don't want to log 404 errors to Sentry.
if (res?.statusCode === 404) {
return errorInitialProps
}

// Running on the server, the response object (`res`) is available.
//
// Next.js will pass an err on the server if a page's data fetching methods
// threw or returned a Promise that rejected
//
// Running on the client (browser), Next.js will provide an err if:
//
// - a page's `getInitialProps` threw or returned a Promise that rejected
// - an exception was thrown somewhere in the React lifecycle (render,
// componentDidMount, etc) that was caught by Next.js's React Error
// Boundary. Read more about what types of exceptions are caught by Error
// Boundaries: https://reactjs.org/docs/error-boundaries.html

if (err) {
Sentry.captureException(err)

// Flushing before returning is necessary if deploying to Vercel, see
// https://vercel.com/docs/platform/limits#streaming-responses
await Sentry.flush(2000)

return errorInitialProps
}

// If this point is reached, getInitialProps was called without any
// information about what the error might be. This is unexpected and may
// indicate a bug introduced in Next.js, so record it in Sentry
Sentry.captureException(
new Error(`_error.js getInitialProps missing data at path: ${asPath}`)
)
await Sentry.flush(2000)

return errorInitialProps
}

export default MyError
5 changes: 4 additions & 1 deletion src/pages/api/bookmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ImageKit from 'imagekit'
import { withSentry } from '@sentry/nextjs'
import prisma from '@/lib/prisma'
import { getSession } from 'next-auth/react'
import { asyncFileReader } from '@/lib/helpers'
Expand All @@ -9,7 +10,7 @@ const metascraper = require('metascraper')([
require('metascraper-title')(),
])

export default async function handler(req, res) {
const handler = async (req, res) => {
const session = await getSession({ req })
const { method, headers, query, body } = req

Expand Down Expand Up @@ -252,3 +253,5 @@ export default async function handler(req, res) {
}
}
}

export default withSentry(handler)

0 comments on commit bb689c9

Please sign in to comment.