From 86c68739e6329535c94e905aae9189fb0839cba2 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Fri, 14 Jan 2022 10:19:43 +0800 Subject: [PATCH] refactor(errors): introduce Slonik Deletion Error (#177) replase all deletion query RequeestError with DeletionError extends SlonikError --- packages/core/src/errors/SlonikError/index.ts | 7 +++++++ packages/core/src/queries/application.ts | 5 +++-- packages/core/src/queries/resources.ts | 9 ++------- packages/core/src/queries/scopes.ts | 9 ++------- packages/core/src/queries/user.ts | 9 ++------- 5 files changed, 16 insertions(+), 23 deletions(-) create mode 100644 packages/core/src/errors/SlonikError/index.ts diff --git a/packages/core/src/errors/SlonikError/index.ts b/packages/core/src/errors/SlonikError/index.ts new file mode 100644 index 000000000..6c70c334d --- /dev/null +++ b/packages/core/src/errors/SlonikError/index.ts @@ -0,0 +1,7 @@ +import { SlonikError } from 'slonik'; + +export class DeletionError extends SlonikError { + public constructor() { + super('Resource not found.'); + } +} diff --git a/packages/core/src/queries/application.ts b/packages/core/src/queries/application.ts index ebd1d291e..864d4b3e9 100644 --- a/packages/core/src/queries/application.ts +++ b/packages/core/src/queries/application.ts @@ -1,11 +1,12 @@ import { Application, ApplicationUpdate, Applications } from '@logto/schemas'; -import { sql, SlonikError } from 'slonik'; +import { sql } from 'slonik'; import { buildFindMany } from '@/database/find-many'; import { buildInsertInto } from '@/database/insert-into'; import pool from '@/database/pool'; import { buildUpdateWhere } from '@/database/update-where'; import { convertToIdentifiers, OmitAutoSetFields, getTotalRowCount } from '@/database/utils'; +import { DeletionError } from '@/errors/SlonikError'; const { table, fields } = convertToIdentifiers(Applications); @@ -48,6 +49,6 @@ export const deleteApplicationById = async (id: string) => { where id=${id} `); if (rowCount < 1) { - throw new SlonikError('Resource not found'); + throw new DeletionError(); } }; diff --git a/packages/core/src/queries/resources.ts b/packages/core/src/queries/resources.ts index 3822c204e..b92881fc0 100644 --- a/packages/core/src/queries/resources.ts +++ b/packages/core/src/queries/resources.ts @@ -5,7 +5,7 @@ import { buildInsertInto } from '@/database/insert-into'; import pool from '@/database/pool'; import { buildUpdateWhere } from '@/database/update-where'; import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils'; -import RequestError from '@/errors/RequestError'; +import { DeletionError } from '@/errors/SlonikError'; const { table, fields } = convertToIdentifiers(Resources); @@ -60,11 +60,6 @@ export const deleteResourceById = async (id: string) => { where id=${id} `); if (rowCount < 1) { - throw new RequestError({ - code: 'entity.not_exists_with_id', - name: Resources.tableSingular, - id, - status: 404, - }); + throw new DeletionError(); } }; diff --git a/packages/core/src/queries/scopes.ts b/packages/core/src/queries/scopes.ts index 2de5b843b..c567d6517 100644 --- a/packages/core/src/queries/scopes.ts +++ b/packages/core/src/queries/scopes.ts @@ -4,7 +4,7 @@ import { sql } from 'slonik'; import { buildInsertInto } from '@/database/insert-into'; import pool from '@/database/pool'; import { convertToIdentifiers } from '@/database/utils'; -import RequestError from '@/errors/RequestError'; +import { DeletionError } from '@/errors/SlonikError'; const { table, fields } = convertToIdentifiers(ResourceScopes); @@ -29,11 +29,6 @@ export const deleteScopeById = async (id: string) => { where id=${id} `); if (rowCount < 1) { - throw new RequestError({ - code: 'entity.not_exists_with_id', - name: ResourceScopes.tableSingular, - id, - status: 404, - }); + throw new DeletionError(); } }; diff --git a/packages/core/src/queries/user.ts b/packages/core/src/queries/user.ts index 72d81e0bc..428dc4d8e 100644 --- a/packages/core/src/queries/user.ts +++ b/packages/core/src/queries/user.ts @@ -5,7 +5,7 @@ import { buildInsertInto } from '@/database/insert-into'; import pool from '@/database/pool'; import { buildUpdateWhere } from '@/database/update-where'; import { convertToIdentifiers, OmitAutoSetFields } from '@/database/utils'; -import RequestError from '@/errors/RequestError'; +import { DeletionError } from '@/errors/SlonikError'; const { table, fields } = convertToIdentifiers(Users); @@ -56,11 +56,6 @@ export const deleteUserById = async (id: string) => { where id=${id} `); if (rowCount < 1) { - throw new RequestError({ - code: 'entity.not_exists_with_id', - name: Users.tableSingular, - id, - status: 404, - }); + throw new DeletionError(); } };