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

mozilla / blurts-server / #13419

pending completion
#13419

push

circleci

flozia
feat: Add client error and handle error classes

282 of 1801 branches covered (15.66%)

Branch coverage included in aggregate %.

79 of 79 new or added lines in 2 files covered. (100.0%)

959 of 4740 relevant lines covered (20.23%)

1.69 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 mozlog from '../utils/log.js'
6
import { MethodNotAllowedError } from '../shared/error.js'
7

8
import { guestLayout } from '../views/guestLayout.js'
9
import { error } from '../views/partials/error.js'
10

11
const log = mozlog('middleware')
×
12

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

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

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

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

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

64
export {
65
  errorHandler,
66
  notFound,
67
  methodNotAllowed
68
}
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