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

mozilla / blurts-server / #11898

pending completion
#11898

push

circleci

web-flow
Merge pull request #2770 from mozilla/license

Add license headers in source files

282 of 1138 branches covered (24.78%)

Branch coverage included in aggregate %.

959 of 3049 relevant lines covered (31.45%)

2.55 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, NotFoundError } from '../utils/error.js'
6
import mozlog from '../utils/log.js'
7
const log = mozlog('middleware')
×
8

9
/**
10
 * Generic error handling function that takes in an error with
11
 *
12
 * message: error message
13
 *
14
 * statusCode: http status code
15
 *
16
 * and returns a json response
17
 * @param {object} err error object [ message, statusCode ]
18
 * @param {object} req request object
19
 * @param {object} res response object
20
 * @param {object} next middleware callback
21
 */
22
function errorHandler (err, req, res, next) {
23
  log.error('error', err.stack)
×
24
  const errStatus = err.statusCode || 500
×
25
  const errMsg = err.message || 'Empty error message'
×
26
  res.status(errStatus).json({
×
27
    success: false,
28
    status: errStatus,
29
    message: process.env.NODE_ENV !== 'production' ? errMsg : 'Something went wrong', // hide error message when in production
×
30
    stack: process.env.NODE_ENV === 'dev' ? err.stack : {} // hide stack when not in dev
×
31
  })
32
}
33

34
/**
35
 * Used as a 404 default for routes
36
 */
37
function notFound (req) {
38
  // TODO: when there's a 404 page to be rendered here,
39
  // replace the json return with html
40
  throw new NotFoundError(`Page not found! ${req.method} ${req.originalUrl}`)
×
41
}
42

43
function methodNotAllowed (req) {
44
  throw new MethodNotAllowedError(`Method not allowed: ${req.method} ${req.originalUrl}`)
×
45
}
46

47
export {
48
  errorHandler,
49
  notFound,
50
  methodNotAllowed
51
}
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