Skip to content

Commit

Permalink
fix(core): avoid unexpected 500 error (#6515)
Browse files Browse the repository at this point in the history
* fix(core): avoid unexpected 500 error

avoid unexpected 500 error

* fix(core): fix ut

fix ut
simeng-li authored Aug 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 53060c2 commit f545716
Showing 4 changed files with 118 additions and 111 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@
"got": "^14.0.0",
"hash-wasm": "^4.11.0",
"helmet": "^7.1.0",
"http-errors": "^2.0.0",
"i18next": "^22.4.15",
"iconv-lite": "0.6.3",
"jose": "^5.6.3",
@@ -101,6 +102,7 @@
"@types/adm-zip": "^0.5.5",
"@types/debug": "^4.1.7",
"@types/etag": "^1.8.1",
"@types/http-errors": "^2.0.4",
"@types/jest": "^29.4.0",
"@types/koa": "^2.15.0",
"@types/koa-compose": "^3.2.5",
6 changes: 2 additions & 4 deletions packages/core/src/middleware/koa-error-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpError } from 'koa';
import createError from 'http-errors';

import RequestError from '#src/errors/RequestError/index.js';
import createMockContext from '#src/test-utils/jest-koa-mocks/create-mock-context.js';
@@ -7,9 +7,7 @@ import koaErrorHandler from './koa-error-handler.js';

const { jest } = import.meta;

// Avoid "TypeError: cannot construct abstract class"
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
const httpError = Object.create(HttpError.prototype);
const httpError = createError(404, 'Not Found');

describe('koaErrorHandler middleware', () => {
const mockBody = { data: 'foo' };
3 changes: 2 additions & 1 deletion packages/core/src/middleware/koa-error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { appInsights } from '@logto/app-insights/node';
import type { RequestErrorBody } from '@logto/schemas';
import { isHttpError } from 'http-errors';
import type { Middleware } from 'koa';
import { HttpError } from 'koa';

@@ -39,7 +40,7 @@ export default function koaErrorHandler<StateT, ContextT, BodyT>(): Middleware<
}

// Koa will handle `HttpError` with a built-in manner.
if (error instanceof HttpError) {
if (error instanceof HttpError || isHttpError(error)) {
return;
}

218 changes: 112 additions & 106 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit f545716

Please sign in to comment.