Skip to content

Commit

Permalink
Add FailBot.report call to events errors (github#17773)
Browse files Browse the repository at this point in the history
* Add FailBot.report call to events errors

* Actually throw the error
  • Loading branch information
JasonEtco authored Feb 9, 2021
1 parent bf907bb commit 6d20e43
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions middleware/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express')
const { omit } = require('lodash')
const Ajv = require('ajv')
const schema = require('../lib/schema-event')
const FailBot = require('../lib/failbot')

const OMIT_FIELDS = ['type', 'token']

Expand All @@ -10,15 +11,17 @@ const ajv = new Ajv()
const router = express.Router()

router.post('/', async (req, res, next) => {
const isDev = process.env.NODE_ENV === 'development'
const referrer = req.get('referrer')
const fields = omit(req.body, '_csrf')

if (!ajv.validate(schema, fields)) {
if (process.env.NODE_ENV === 'development') console.log(ajv.errorsText())
if (isDev) console.log(ajv.errorsText())
return res.status(400).json({})
}

// Don't depend on Hydro on local development
if (process.env.NODE_ENV === 'development' && !req.hydro.maySend()) {
if (isDev && !req.hydro.maySend()) {
return res.status(200).json({})
}

Expand All @@ -27,10 +30,22 @@ router.post('/', async (req, res, next) => {
req.hydro.schemas[fields.type],
omit(fields, OMIT_FIELDS)
)
if (!hydroRes.ok) return res.status(502).json({})

if (!hydroRes.ok) {
const err = new Error('Hydro request failed')
err.status = hydroRes.status
throw err
}

return res.status(201).json(fields)
} catch (err) {
if (process.env.NODE_ENV === 'development') console.log(err)
if (isDev) console.error(err)

await FailBot.report(err, {
path: req.path,
referrer
})

return res.status(502).json({})
}
})
Expand Down

0 comments on commit 6d20e43

Please sign in to comment.