Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #282 Custom error handler not being reached #285

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
});
});
Loading