Skip to content

Commit

Permalink
Merge pull request #285 from htdangkhoa/develop
Browse files Browse the repository at this point in the history
fix: #282 Custom error handler not being reached
  • Loading branch information
htdangkhoa committed Nov 29, 2023
2 parents 4afe80f + 882abac commit e4308f0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node: ['10', '12', '14', '15']
node: ['10', '12', '14', '16', '18']

name: Node ${{ matrix.node }} testing

Expand Down
2 changes: 1 addition & 1 deletion demos/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ app.use((error, req, res, next) => {
res.send('test.', 500);
});

app.listen(4000, () => console.log('Server is listening on port 3000...'));
app.listen(3000, () => console.log('Server is listening on port 3000...'));
14 changes: 11 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ class Router {
const router = routers[i];

for (let j = 0; j < router.routes.length; j += 1) {
const { path: childPath, middlewarePath, fns, method } = router.routes[
j
];
const {
path: childPath,
middlewarePath,
fns,
method,
} = router.routes[j];

const path = (route + (childPath || middlewarePath || '')).replace(
/\/{1,}/g,
Expand Down Expand Up @@ -210,6 +213,11 @@ class Router {
if (typeof fn === 'function' && fn.length === 4)
return fn(error, req, res, next);

const nextFn = handlers[id];
if (typeof nextFn === 'function' && nextFn.length === 4) {
return nextFn(error, req, res, next);
}

return res.send(error.stack, error.status || 500);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/error-handling/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const pureHttp = require('../..');

module.exports = function () {
const app = pureHttp();

app.get(
'/custom',
(_req, _res, _next) => {
throw new Error('bad');
},
/* istanbul ignore next */
// ignore coverage for this function because it will never be called.
(req, res, _next) => {
res.send('good');
},
);

app.use((error, req, res, _next) => {
res.send('Something went wrong.', 500);
});

return app;
};
16 changes: 16 additions & 0 deletions tests/error-handling/custom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable global-require */
/* eslint-disable no-unused-vars */
const supertest = require('supertest');

describe('ALL /not-found', () => {
it(`The status should be 404.`, async () => {
const app = require('./custom')();

const request = supertest(app);

const res = await request.get('/custom');

expect(res.statusCode).toBe(500);
expect(res.text).toBe('Something went wrong.');
});
});

0 comments on commit e4308f0

Please sign in to comment.