Passed
Push — main ( 74e679...90c63f )
by Julia
01:01 queued 20s
created

error-handler.js ➔ errorHandler   A

Complexity

Conditions 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
c 0
b 0
f 0
rs 9.95
cc 2
1
import express from "express";
2
3
/**
4
 *
5
 * @param {Error} err
6
 * @param {express.Request} req
7
 * @param {express.Response} res
8
 * @param {express.NextFunction} next
9
 */
10
// eslint-disable-next-line no-unused-vars
11
function errorHandler(err, req, res, next) {
12
    if (res.headersSent) {
13
        return next(err);
14
    }
15
16
    console.error(err.stack);
17
18
    const statusCode = res.statusCode || 500;
19
20
    res.status(statusCode).json({
21
        errors: {
22
            message: err.message,
23
            code: statusCode
24
        }
25
    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
26
}
27
28
export default errorHandler;
29