Commit 02966afd authored by Václav Nosek's avatar Václav Nosek Committed by Tim Neutkens

[with-sentry] example: Make 'utils/sentry.js' more flexible (#6769)

* Make `ctx` optional in `captureException` function

* Make 'release' optional when calling sentry util

* Fix code style
parent 18884676
import App from 'next/app'
import sentry from '../utils/sentry'
const { Sentry, captureException } = sentry({ release: process.env.SENTRY_RELEASE })
const { Sentry, captureException } = sentry()
export default class MyApp extends App {
constructor () {
......
......@@ -40,7 +40,7 @@ const sourcemapsForSentryOnly = token => (req, res, next) => {
app.prepare().then(() => {
// The app.buildId is only available after app.prepare(), hence why we setup
// here.
const { Sentry } = require('./utils/sentry')({ release: app.buildId })
const { Sentry } = require('./utils/sentry')(app.buildId)
express()
// This attaches request information to sentry errors
......
......@@ -3,7 +3,7 @@
const Sentry = require('@sentry/node')
const Cookie = require('js-cookie')
module.exports = ({ release }) => {
module.exports = (release = process.env.SENTRY_RELEASE) => {
const sentryOptions = {
dsn: process.env.SENTRY_DSN,
release,
......@@ -31,7 +31,7 @@ module.exports = ({ release }) => {
return {
Sentry,
captureException: (err, { req, res, errorInfo, query, pathname }) => {
captureException: (err, ctx) => {
Sentry.configureScope((scope) => {
if (err.message) {
// De-duplication currently doesn't work correctly for SSR / browser errors
......@@ -43,36 +43,40 @@ module.exports = ({ release }) => {
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)
if (ctx) {
const { req, res, errorInfo, query, pathname } = ctx
// On client-side we use js-cookie package to fetch it
const sessionId = Cookie.get('sid')
if (sessionId) {
scope.setUser({ id: sessionId })
if (res && res.statusCode) {
scope.setExtra('statusCode', res.statusCode)
}
} 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 (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) {
Object.keys(errorInfo).forEach(key => scope.setExtra(key, errorInfo[key]))
if (errorInfo) {
Object.keys(errorInfo).forEach(key => scope.setExtra(key, errorInfo[key]))
}
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment