• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / blurts-server / 45ba77d7-fd3c-4064-90eb-157d6aa10611

pending completion
45ba77d7-fd3c-4064-90eb-157d6aa10611

push

circleci

Robert Helmer
update the queue adr

282 of 1631 branches covered (17.29%)

Branch coverage included in aggregate %.

959 of 4375 relevant lines covered (21.92%)

3.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/middleware/error.js
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4

5
import { MethodNotAllowedError } from '../utils/error.js'
6
import mozlog from '../utils/log.js'
7
import { guestLayout } from '../views/guestLayout.js'
8
import { error } from '../views/partials/error.js'
9
const log = mozlog('middleware')
×
10

11
/**
12
 * Generic error handling function that takes in an error with
13
 *
14
 * message: error message
15
 *
16
 * statusCode: http status code
17
 *
18
 * and returns a json response
19
 *
20
 * @param {object} err error object [ message, statusCode ]
21
 * @param {object} req request object
22
 * @param {object} res response object
23
 * @param {object} next middleware callback
24
 */
25
function errorHandler (err, req, res, next) {
26
  log.error('error', err.stack)
×
27
  const errStatus = err.statusCode || 500
×
28
  const errMsg = err.message || 'Empty error message'
×
29

30
  if (req.accepts('text/html') === 'text/html') {
×
31
    res.status(errStatus).send(guestLayout({
×
32
      partial: error,
33
      nonce: res.locals.nonce,
34
      statusCode: errStatus
35
    }))
36
    return
×
37
  }
38

39
  res.status(errStatus).json({
×
40
    success: false,
41
    status: errStatus,
42
    message: process.env.NODE_ENV !== 'production' ? errMsg : 'Something went wrong', // hide error message when in production
×
43
    stack: process.env.NODE_ENV === 'dev' ? err.stack : {} // hide stack when not in dev
×
44
  })
45
}
46

47
/**
48
 * Log 404 errors, but don't send a response - they're handled by the `notFound` view
49
 *
50
 * @param req
51
 * @param _res
52
 * @param next
53
 */
54
function notFound (req, _res, next) {
55
  log.info('http-error', { statusCode: 404, method: req.method, originalUrl: req.originalUrl })
×
56
  next()
×
57
}
58

59
function methodNotAllowed (req) {
60
  throw new MethodNotAllowedError(`Method not allowed: ${req.method} ${req.originalUrl}`)
×
61
}
62

63
export {
64
  errorHandler,
65
  notFound,
66
  methodNotAllowed
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc