Skip to content

Commit

Permalink
Enable sentry error capturing on server
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBenc committed Nov 9, 2020
1 parent a4bf027 commit 5dfec24
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion server/emailSubmitter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('isomorphic-fetch')
const {backendConfig} = require('./helpers/loadConfig')
const {captureException} = require('@sentry/node')

module.exports = function(app, env) {
app.post('/api/emails/submit', async (req, res) => {
Expand Down Expand Up @@ -49,8 +50,9 @@ module.exports = function(app, env) {
Left: 'Email submission rejected by network',
})
} catch (err) {
captureException(err)
return res.json({
Left: 'An unexpected error has happened',
Left: 'An unexpected error has occurred.',
})
}
})
Expand Down
7 changes: 7 additions & 0 deletions server/helpers/dropSensitiveEventData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function dropSensitiveEventData(event) {
if (event.request && event.request.data) delete event.request.data
if (event.request && event.request.cookies) delete event.request.cookies
// otherwise all errors would have generic names
event.exception.values[0].type = event.exception.values[0].value
return event
}
13 changes: 13 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ const ipfilter = require('express-ipfilter').IpFilter
const errorHandler = require('./middlewares/errorHandler')

let app = express()
const Sentry = require('@sentry/node')
const dropSensitiveEventData = require('./helpers/dropSensitiveEventData')

Sentry.init({
dsn: 'https://43eac31915bb40caa03798a51048e756@o150853.ingest.sentry.io/5421403',
tracesSampleRate: 0,
beforeSend(event) {
return dropSensitiveEventData(event)
},
})

app.use(Sentry.Handlers.requestHandler())

express.static.mime.types.wasm = 'application/wasm'

Expand Down Expand Up @@ -114,6 +126,7 @@ app.get('*', (req, res) => {
`)
})

app.use(Sentry.Handlers.errorHandler())
app.use(errorHandler)

/*
Expand Down
2 changes: 2 additions & 0 deletions server/middlewares/statsGoogleAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const normalizeUrl = require('normalize-url')
const {parseTxBodyOutAmount, parseTxBodyTotalAmount} = require('../helpers/parseTxBody')
const ua = require('universal-analytics')
const {backendConfig} = require('../helpers/loadConfig')
const {captureException} = require('@sentry/node')

const knownIps = new Set()

Expand Down Expand Up @@ -113,6 +114,7 @@ const trackTxSubmissions = mung.jsonAsync(async (body, req, res) => {
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Tracking event failed - ${err}`)
captureException(err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions server/middlewares/statsRedis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const client = redis.createClient(process.env.REDIS_URL)
const mung = require('express-mung')
const normalizeUrl = require('normalize-url')
const {parseTxBodyOutAmount, parseTxBodyTotalAmount} = require('../helpers/parseTxBody')
const {captureException} = require('@sentry/node')

const knownIps = new Set()

Expand Down Expand Up @@ -66,6 +67,7 @@ const trackTxSubmissions = mung.json((body, req, res) => {
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
captureException(e)
}

incrCountersBy(`${txSubmissionType}:sentOut`, txOutAmount)
Expand Down
2 changes: 2 additions & 0 deletions server/statsPageRedis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const redis = require('redis')
const redisScan = require('redisscan')
const client = redis.createClient(process.env.REDIS_URL)
const {captureException} = require('@sentry/node')

const getStats = async () => {
const stats = {
Expand Down Expand Up @@ -161,6 +162,7 @@ module.exports = function(app, env) {
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
captureException(e)
return 'Something went wrong, bother the devs.'
}
})
Expand Down
8 changes: 7 additions & 1 deletion server/transactionSubmitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('isomorphic-fetch')
const Sentry = require('@sentry/node')

module.exports = function(app, env) {
// eslint-disable-next-line consistent-return
Expand Down Expand Up @@ -37,19 +38,24 @@ module.exports = function(app, env) {
}

const errorMessage = await response.text()

// eslint-disable-next-line no-console
console.error(
`Submission of tx ${txHash} failed with status ${response.status} and message ${errorMessage}`
)

Sentry.captureException(new Error('TransactionSubmissionFailed'), {
contexts: [{...JSON.parse(errorMessage)}],
})

return res.json({
Left: `Transaction rejected by network - ${errorMessage}`,
statusCode: response.status,
})
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Submission of tx ${txHash} failed with an unexpected error: ${err.stack}`)

Sentry.captureException(e)
return res.json({
Left: 'An unexpected error has occurred',
})
Expand Down

0 comments on commit 5dfec24

Please sign in to comment.