From be0d9326e468915a2222f9511e3937e5eb844005 Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Fri, 8 Jul 2022 17:19:53 +0800 Subject: [PATCH] refactor(core): remove InvalidInputError (#1477) --- .../core/src/middleware/koa-slonik-error-handler.ts | 1 - packages/core/src/routes/admin-user.test.ts | 2 +- packages/core/src/routes/admin-user.ts | 10 +++++++--- packages/phrases/src/locales/en.ts | 1 + packages/phrases/src/locales/zh-cn.ts | 1 + 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/core/src/middleware/koa-slonik-error-handler.ts b/packages/core/src/middleware/koa-slonik-error-handler.ts index 6f8f4fe3b..5a8c5886c 100644 --- a/packages/core/src/middleware/koa-slonik-error-handler.ts +++ b/packages/core/src/middleware/koa-slonik-error-handler.ts @@ -8,7 +8,6 @@ * ForeignKeyIntegrityConstraintViolationError, * IntegrityConstraintViolationError, * InvalidConfigurationError, - * InvalidInputError, * NotFoundError, * NotNullIntegrityConstraintViolationError, * StatementCancelledError, diff --git a/packages/core/src/routes/admin-user.test.ts b/packages/core/src/routes/admin-user.test.ts index ecb06f53c..24f21c323 100644 --- a/packages/core/src/routes/admin-user.test.ts +++ b/packages/core/src/routes/admin-user.test.ts @@ -234,7 +234,7 @@ describe('adminUserRoutes', () => { ); await expect( userRequest.patch('/users/foo').send({ roleNames: ['admin'] }) - ).resolves.toHaveProperty('status', 500); + ).resolves.toHaveProperty('status', 400); expect(findUserById).toHaveBeenCalledTimes(1); expect(updateUserById).not.toHaveBeenCalled(); }); diff --git a/packages/core/src/routes/admin-user.ts b/packages/core/src/routes/admin-user.ts index ebf5a280f..f319488fb 100644 --- a/packages/core/src/routes/admin-user.ts +++ b/packages/core/src/routes/admin-user.ts @@ -2,7 +2,6 @@ import { arbitraryObjectGuard, userInfoSelectFields } from '@logto/schemas'; import { passwordRegEx, usernameRegEx } from '@logto/shared'; import { has } from '@silverhand/essentials'; import pick from 'lodash.pick'; -import { InvalidInputError } from 'slonik'; import { literal, object, string } from 'zod'; import RequestError from '@/errors/RequestError'; @@ -134,8 +133,13 @@ export default function adminUserRoutes(router: T) { const resourcesNotFound = roleNames.filter( (roleName) => !roles.some(({ name }) => roleName === name) ); - // TODO: Should be cached by the error handler and return request error - throw new InvalidInputError(`role names (${resourcesNotFound.join(',')}) are not valid`); + throw new RequestError({ + status: 400, + code: 'user.invalid_role_names', + data: { + roleNames: resourcesNotFound.join(','), + }, + }); } } diff --git a/packages/phrases/src/locales/en.ts b/packages/phrases/src/locales/en.ts index 5916786f9..52dbf35be 100644 --- a/packages/phrases/src/locales/en.ts +++ b/packages/phrases/src/locales/en.ts @@ -579,6 +579,7 @@ const errors = { phone_not_exists: 'The phone number has not been registered yet.', identity_not_exists: 'The social account has not been registered yet.', identity_exists: 'The social account has been registered.', + invalid_role_names: 'role names ({{roleNames}}) are not valid', }, password: { unsupported_encryption_method: 'The encryption method {{name}} is not supported.', diff --git a/packages/phrases/src/locales/zh-cn.ts b/packages/phrases/src/locales/zh-cn.ts index 8e60216b3..2f6146c51 100644 --- a/packages/phrases/src/locales/zh-cn.ts +++ b/packages/phrases/src/locales/zh-cn.ts @@ -557,6 +557,7 @@ const errors = { phone_not_exists: '手机号码尚未注册', identity_not_exists: '该社交帐号尚未注册', identity_exists: '该社交帐号已被注册', + invalid_role_names: '角色名称({{roleNames}})无效', }, password: { unsupported_encryption_method: '不支持的加密方法 {{name}}',