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 App from 'next/app'
import sentry from '../utils/sentry' import sentry from '../utils/sentry'
const { Sentry, captureException } = sentry({ release: process.env.SENTRY_RELEASE }) const { Sentry, captureException } = sentry()
export default class MyApp extends App { export default class MyApp extends App {
constructor () { constructor () {
......
...@@ -40,7 +40,7 @@ const sourcemapsForSentryOnly = token => (req, res, next) => { ...@@ -40,7 +40,7 @@ const sourcemapsForSentryOnly = token => (req, res, next) => {
app.prepare().then(() => { app.prepare().then(() => {
// The app.buildId is only available after app.prepare(), hence why we setup // The app.buildId is only available after app.prepare(), hence why we setup
// here. // here.
const { Sentry } = require('./utils/sentry')({ release: app.buildId }) const { Sentry } = require('./utils/sentry')(app.buildId)
express() express()
// This attaches request information to sentry errors // This attaches request information to sentry errors
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
const Sentry = require('@sentry/node') const Sentry = require('@sentry/node')
const Cookie = require('js-cookie') const Cookie = require('js-cookie')
module.exports = ({ release }) => { module.exports = (release = process.env.SENTRY_RELEASE) => {
const sentryOptions = { const sentryOptions = {
dsn: process.env.SENTRY_DSN, dsn: process.env.SENTRY_DSN,
release, release,
...@@ -31,7 +31,7 @@ module.exports = ({ release }) => { ...@@ -31,7 +31,7 @@ module.exports = ({ release }) => {
return { return {
Sentry, Sentry,
captureException: (err, { req, res, errorInfo, query, pathname }) => { captureException: (err, ctx) => {
Sentry.configureScope((scope) => { Sentry.configureScope((scope) => {
if (err.message) { if (err.message) {
// De-duplication currently doesn't work correctly for SSR / browser errors // De-duplication currently doesn't work correctly for SSR / browser errors
...@@ -43,6 +43,9 @@ module.exports = ({ release }) => { ...@@ -43,6 +43,9 @@ module.exports = ({ release }) => {
scope.setExtra('statusCode', err.statusCode) scope.setExtra('statusCode', err.statusCode)
} }
if (ctx) {
const { req, res, errorInfo, query, pathname } = ctx
if (res && res.statusCode) { if (res && res.statusCode) {
scope.setExtra('statusCode', res.statusCode) scope.setExtra('statusCode', res.statusCode)
} }
...@@ -74,6 +77,7 @@ module.exports = ({ release }) => { ...@@ -74,6 +77,7 @@ module.exports = ({ release }) => {
if (errorInfo) { if (errorInfo) {
Object.keys(errorInfo).forEach(key => scope.setExtra(key, errorInfo[key])) Object.keys(errorInfo).forEach(key => scope.setExtra(key, errorInfo[key]))
} }
}
}) })
return Sentry.captureException(err) return Sentry.captureException(err)
......
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