Skip to content

Commit

Permalink
fix(ErrorMiddleware): Support multiple http-errors versions
Browse files Browse the repository at this point in the history
The `http-errors` handling in v7.0.0 may have not worked when multiple
versions of the module were present.
  • Loading branch information
72636c committed Aug 28, 2023
1 parent 591fb75 commit e9e723b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
{
extends: ['github>seek-oss/rynovate'],
packageRules: [
{
matchManagers: ['npm'],
matchPackageNames: [
// Matches transitive Koa dependency
'http-errors',
],
matchUpdateTypes: ['major'],

enabled: false,
},
],
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"test": "skuba test",
"test:ci": "skuba test --coverage"
},
"dependencies": {},
"dependencies": {
"http-errors": "^1.8.0"
},
"devDependencies": {
"@koa/router": "12.0.0",
"@types/koa": "2.13.8",
Expand Down
22 changes: 22 additions & 0 deletions src/errorMiddleware/errorMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ describe('errorMiddleware', () => {
await agent.get('/').expect(400, 'Badness!');
});

it('respects status from a http-errors-compatible error', async () => {
mockNext.mockImplementation(() => {
throw Object.assign(new Error('Badness!'), {
expose: false,
status: 418,
statusCode: 418,
});
});

await agent.get('/').expect(418, 'Badness!');

mockNext.mockImplementation(() => {
throw Object.assign(new Error('Badness!'), {
expose: true,
status: 400,
statusCode: 400,
});
});

await agent.get('/').expect(400, 'Badness!');
});

it('respects status if isJsonResponse is present', async () => {
class JsonResponseError extends Error {
constructor(
Expand Down
5 changes: 3 additions & 2 deletions src/errorMiddleware/errorMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Context, HttpError, type Middleware } from 'koa';
import { isHttpError } from 'http-errors';
import type { Context, Middleware } from 'koa';

/**
* @see {@link https://github.com/microsoft/TypeScript/issues/1863}
Expand Down Expand Up @@ -80,7 +81,7 @@ export const handle: Middleware = async (ctx, next) => {
if (
!isObject(err) ||
typeof err.status !== 'number' ||
(!(err instanceof HttpError) && err.isJsonResponse === undefined)
(!isHttpError(err) && err.isJsonResponse === undefined)
) {
ctx.status = 500;
ctx.body = '';
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4129,7 +4129,7 @@ http-cache-semantics@^4.1.1:
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a"
integrity sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ==

http-errors@^1.6.3, http-errors@~1.8.0:
http-errors@^1.6.3, http-errors@^1.8.0, http-errors@~1.8.0:
version "1.8.1"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.8.1.tgz#7c3f28577cbc8a207388455dbd62295ed07bd68c"
integrity sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==
Expand Down

0 comments on commit e9e723b

Please sign in to comment.