-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve with-sentry example * remove nonexisting keys from request and update errorInfo handling * readd query and pathname * read query and params and add pathname and query to client
- Loading branch information
1 parent
c867b0c
commit cd1d364
Showing
7 changed files
with
219 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const webpack = require('webpack') | ||
const nextSourceMaps = require('@zeit/next-source-maps')() | ||
|
||
const SENTRY_DSN = '' | ||
|
||
module.exports = nextSourceMaps({ | ||
webpack: (config, { dev, isServer, buildId }) => { | ||
if (!dev) { | ||
config.plugins.push( | ||
new webpack.DefinePlugin({ | ||
'process.env.SENTRY_DSN': JSON.stringify(SENTRY_DSN), | ||
'process.env.SENTRY_RELEASE': JSON.stringify(buildId) | ||
}) | ||
) | ||
} | ||
|
||
if (!isServer) { | ||
config.resolve.alias['@sentry/node'] = '@sentry/browser' | ||
} | ||
|
||
return config | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import App from 'next/app' | ||
import * as Sentry from '@sentry/browser' | ||
import { captureException } from '../utils/sentry' | ||
|
||
const SENTRY_PUBLIC_DSN = '' | ||
class MyApp extends App { | ||
// This reports errors before rendering, when fetching initial props | ||
static async getInitialProps (appContext) { | ||
const { Component, ctx } = appContext | ||
|
||
export default class MyApp extends App { | ||
constructor (...args) { | ||
super(...args) | ||
Sentry.init({dsn: SENTRY_PUBLIC_DSN}) | ||
let pageProps = {} | ||
|
||
try { | ||
if (Component.getInitialProps) { | ||
pageProps = await Component.getInitialProps(ctx) | ||
} | ||
} catch (e) { | ||
captureException(e, ctx) | ||
throw e // you can also skip re-throwing and set property on pageProps | ||
} | ||
|
||
return { | ||
pageProps | ||
} | ||
} | ||
|
||
// This reports errors thrown while rendering components | ||
componentDidCatch (error, errorInfo) { | ||
Sentry.configureScope(scope => { | ||
Object.keys(errorInfo).forEach(key => { | ||
scope.setExtra(key, errorInfo[key]) | ||
}) | ||
}) | ||
Sentry.captureException(error) | ||
|
||
// This is needed to render errors correctly in development / production | ||
captureException(error, { errorInfo }) | ||
super.componentDidCatch(error, errorInfo) | ||
} | ||
} | ||
|
||
export default MyApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const next = require('next') | ||
const express = require('express') | ||
const cookieParser = require('cookie-parser') | ||
const Sentry = require('@sentry/node') | ||
const uuidv4 = require('uuid/v4') | ||
const port = parseInt(process.env.PORT, 10) || 3000 | ||
const dev = process.env.NODE_ENV !== 'production' | ||
const app = next({ dev }) | ||
const handle = app.getRequestHandler() | ||
|
||
require('./utils/sentry') | ||
|
||
app.prepare() | ||
.then(() => { | ||
const server = express() | ||
|
||
// This attaches request information to sentry errors | ||
server.use(Sentry.Handlers.requestHandler()) | ||
|
||
server.use(cookieParser()) | ||
|
||
server.use((req, res, next) => { | ||
const htmlPage = | ||
!req.path.match(/^\/(_next|static)/) && | ||
!req.path.match(/\.(js|map)$/) && | ||
req.accepts('text/html', 'text/css', 'image/png') === 'text/html' | ||
|
||
if (!htmlPage) { | ||
next() | ||
return | ||
} | ||
|
||
if (!req.cookies.sid || req.cookies.sid.length === 0) { | ||
req.cookies.sid = uuidv4() | ||
res.cookie('sid', req.cookies.sid) | ||
} | ||
|
||
next() | ||
}) | ||
|
||
// In production we don't want to serve sourcemaps for anyone | ||
if (!dev) { | ||
const hasSentryToken = !!process.env.SENTRY_TOKEN | ||
server.get(/\.map$/, (req, res, next) => { | ||
if (hasSentryToken && req.headers['x-sentry-token'] !== process.env.SENTRY_TOKEN) { | ||
res | ||
.status(401) | ||
.send( | ||
'Authentication access token is required to access the source map.' | ||
) | ||
return | ||
} | ||
next() | ||
}) | ||
} | ||
|
||
server.get('*', (req, res) => handle(req, res)) | ||
|
||
// This handles errors if they are thrown before raching the app | ||
server.use(Sentry.Handlers.errorHandler()) | ||
|
||
server.listen(port, err => { | ||
if (err) throw err | ||
console.log(`> Ready on http://localhost:${port}`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const Sentry = require('@sentry/node') | ||
const Cookie = require('js-cookie') | ||
|
||
if (process.env.SENTRY_DSN) { | ||
Sentry.init({ | ||
dsn: process.env.SENTRY_DSN, | ||
release: process.env.SENTRY_RELEASE, | ||
maxBreadcrumbs: 50, | ||
attachStacktrace: true | ||
}) | ||
} | ||
|
||
function captureException (err, { req, res, errorInfo, query, pathname }) { | ||
Sentry.configureScope(scope => { | ||
if (err.message) { | ||
// De-duplication currently doesn't work correctly for SSR / browser errors | ||
// so we force deduplication by error message if it is present | ||
scope.setFingerprint([err.message]) | ||
} | ||
|
||
if (err.statusCode) { | ||
scope.setExtra('statusCode', err.statusCode) | ||
} | ||
|
||
if (res && res.statusCode) { | ||
scope.setExtra('statusCode', res.statusCode) | ||
} | ||
|
||
if (process.browser) { | ||
scope.setTag('ssr', false) | ||
scope.setExtra('query', query) | ||
scope.setExtra('pathname', pathname) | ||
|
||
// On client-side we use js-cookie package to fetch it | ||
const sessionId = Cookie.get('sid') | ||
if (sessionId) { | ||
scope.setUser({ id: sessionId }) | ||
} | ||
} else { | ||
scope.setTag('ssr', true) | ||
scope.setExtra('url', req.url) | ||
scope.setExtra('method', req.method) | ||
scope.setExtra('headers', req.headers) | ||
scope.setExtra('params', req.params) | ||
scope.setExtra('query', req.query) | ||
|
||
// On server-side we take session cookie directly from request | ||
if (req.cookies.sid) { | ||
scope.setUser({ id: req.cookies.sid }) | ||
} | ||
} | ||
|
||
if (errorInfo) { | ||
scope.setExtra('componentStack', errorInfo.componentStack) | ||
} | ||
}) | ||
|
||
Sentry.captureException(err) | ||
} | ||
|
||
module.exports = { | ||
captureException | ||
} |