mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor: display oidc errors in json, close #34
This commit is contained in:
parent
ae6e196f37
commit
9befa9c5f6
4 changed files with 41 additions and 3 deletions
|
@ -15,6 +15,7 @@
|
|||
"@logto/essentials": "^1.0.7",
|
||||
"@logto/schemas": "^1.1.0-rc.0",
|
||||
"dayjs": "^1.10.5",
|
||||
"decamelize": "^5.0.0",
|
||||
"dotenv": "^10.0.0",
|
||||
"formidable": "^1.2.2",
|
||||
"got": "^11.8.2",
|
||||
|
@ -25,6 +26,7 @@
|
|||
"koa-mount": "^4.0.0",
|
||||
"koa-proxies": "^0.12.1",
|
||||
"koa-router": "^10.0.0",
|
||||
"lodash.pick": "^4.4.0",
|
||||
"module-alias": "^2.2.2",
|
||||
"nanoid": "^3.1.23",
|
||||
"oidc-provider": "^7.4.1",
|
||||
|
@ -40,6 +42,7 @@
|
|||
"@types/koa-logger": "^3.1.1",
|
||||
"@types/koa-mount": "^4.0.0",
|
||||
"@types/koa-router": "^7.4.2",
|
||||
"@types/lodash.pick": "^4.4.6",
|
||||
"@types/oidc-provider": "^7.4.0",
|
||||
"husky": "^6.0.0",
|
||||
"tsc-watch": "^4.4.0",
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import pick from 'lodash.pick';
|
||||
|
||||
export enum GuardErrorCode {
|
||||
InvalidInput = 'guard.invalid_input',
|
||||
}
|
||||
|
@ -18,13 +20,13 @@ export type RequestErrorMetadata = {
|
|||
status?: number;
|
||||
};
|
||||
|
||||
export type RequestErrorBody = { message: string; data: unknown };
|
||||
export type RequestErrorBody = { message: string; data: unknown; code: string };
|
||||
|
||||
export default class RequestError extends Error {
|
||||
code: RequestErrorCode;
|
||||
status: number;
|
||||
expose: boolean;
|
||||
body: RequestErrorBody;
|
||||
data: unknown;
|
||||
|
||||
constructor(input: RequestErrorMetadata | RequestErrorCode, data?: unknown) {
|
||||
const { code, status = 400 } = typeof input === 'string' ? { code: input } : input;
|
||||
|
@ -35,6 +37,10 @@ export default class RequestError extends Error {
|
|||
this.expose = true;
|
||||
this.code = code;
|
||||
this.status = status;
|
||||
this.body = { message, data };
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
get body(): RequestErrorBody {
|
||||
return pick(this, 'message', 'code', 'data');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import RequestError, { RequestErrorBody } from '@/errors/RequestError';
|
||||
import decamelize from 'decamelize';
|
||||
import { Middleware } from 'koa';
|
||||
import { errors } from 'oidc-provider';
|
||||
|
||||
export default function koaErrorHandler<StateT, ContextT>(): Middleware<
|
||||
StateT,
|
||||
|
@ -16,6 +18,16 @@ export default function koaErrorHandler<StateT, ContextT>(): Middleware<
|
|||
return;
|
||||
}
|
||||
|
||||
if (error instanceof errors.OIDCProviderError) {
|
||||
ctx.status = error.status;
|
||||
ctx.body = {
|
||||
message: error.error_description ?? error.message,
|
||||
code: `oidc.${decamelize(error.name)}`,
|
||||
data: error.error_detail,
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -632,6 +632,18 @@
|
|||
"@types/koa-compose" "*"
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/lodash.pick@^4.4.6":
|
||||
version "4.4.6"
|
||||
resolved "https://registry.yarnpkg.com/@types/lodash.pick/-/lodash.pick-4.4.6.tgz#ae4e8f109e982786313bb6aac4b1a73aefa6e9be"
|
||||
integrity sha512-u8bzA16qQ+8dY280z3aK7PoWb3fzX5ATJ0rJB6F+uqchOX2VYF02Aqa+8aYiHiHgPzQiITqCgeimlyKFy4OA6g==
|
||||
dependencies:
|
||||
"@types/lodash" "*"
|
||||
|
||||
"@types/lodash@*":
|
||||
version "4.14.170"
|
||||
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.170.tgz#0d67711d4bf7f4ca5147e9091b847479b87925d6"
|
||||
integrity sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q==
|
||||
|
||||
"@types/mime@^1":
|
||||
version "1.3.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
|
||||
|
@ -1552,6 +1564,11 @@ decamelize@^1.1.0, decamelize@^1.2.0:
|
|||
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
|
||||
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=
|
||||
|
||||
decamelize@^5.0.0:
|
||||
version "5.0.0"
|
||||
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-5.0.0.tgz#88358157b010ef133febfd27c18994bd80c6215b"
|
||||
integrity sha512-U75DcT5hrio3KNtvdULAWnLiAPbFUC4191ldxMmj4FA/mRuBnmDwU0boNfPyFRhnan+Jm+haLeSn3P0afcBn4w==
|
||||
|
||||
decode-uri-component@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
|
||||
|
|
Loading…
Reference in a new issue