From 9befa9c5f607b8aa21973058b8ec5780e3d5bb2a Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Tue, 6 Jul 2021 23:29:36 +0800 Subject: [PATCH] refactor: display oidc errors in json, close #34 --- packages/core/package.json | 3 +++ packages/core/src/errors/RequestError.ts | 12 +++++++++--- .../core/src/middleware/koa-error-handler.ts | 12 ++++++++++++ packages/core/yarn.lock | 17 +++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 877e96f17..c5fceb7c2 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/errors/RequestError.ts b/packages/core/src/errors/RequestError.ts index 444cdc092..d07d04fb6 100644 --- a/packages/core/src/errors/RequestError.ts +++ b/packages/core/src/errors/RequestError.ts @@ -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'); } } diff --git a/packages/core/src/middleware/koa-error-handler.ts b/packages/core/src/middleware/koa-error-handler.ts index b856096a8..cd22f2797 100644 --- a/packages/core/src/middleware/koa-error-handler.ts +++ b/packages/core/src/middleware/koa-error-handler.ts @@ -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(): Middleware< StateT, @@ -16,6 +18,16 @@ export default function koaErrorHandler(): 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; } }; diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock index 9fb9269bd..3b1df9cab 100644 --- a/packages/core/yarn.lock +++ b/packages/core/yarn.lock @@ -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"