Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 3748b39

Browse files
refactor: simplify creating HTTP errors
The http-errors module doesn't create a new error if it is already an HTTP error, so we don't need to check its type locally.
1 parent fa2467e commit 3748b39

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/middleware/error-handler.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createError, { HttpError, UnknownError } from 'http-errors';
1+
import createHttpError, { HttpError } from 'http-errors';
22
import {
33
DefaultStateExtends, ExtendableContext, Middleware, Next,
44
} from 'koa';
@@ -32,16 +32,12 @@ const handleHttpError = (
3232
response.dataset = dataset(quads);
3333
};
3434

35-
const toHttpError = (error: UnknownError): HttpError => (
36-
error instanceof HttpError ? error : createError(error)
37-
);
38-
3935
export default (): Middleware<DefaultStateExtends, DatasetContext<ExtendableContext>> => (
4036
async (context: DatasetContext<ExtendableContext>, next: Next): Promise<void> => {
4137
try {
4238
await next();
4339
} catch (error) {
44-
handleHttpError(toHttpError(error), context);
40+
handleHttpError(createHttpError(error), context);
4541

4642
context.app.emit('error', error, context);
4743
}

0 commit comments

Comments
 (0)