0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-10 22:22:45 -05:00

feat(application): add application query exceptions (#172)

* feat(application): add application query exceptions

add application query exceptions

* fix(core): cr fix remove query level exception

remove application query level exception handle logic

* fix(core): delete application query throw slonikError

delete application query throw slonikError
This commit is contained in:
simeng-li 2022-01-13 16:02:28 +08:00 committed by GitHub
parent 77be675bfb
commit f218667ba4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 8 deletions

View file

@ -3,6 +3,7 @@ import { RequestErrorBody } from '@logto/schemas';
import decamelize from 'decamelize';
import { Middleware } from 'koa';
import { errors } from 'oidc-provider';
import { NotFoundError } from 'slonik';
import RequestError from '@/errors/RequestError';
@ -32,6 +33,13 @@ export default function koaErrorHandler<StateT, ContextT>(): Middleware<
return;
}
if (error instanceof NotFoundError) {
const error = new RequestError({ code: 'entity.not_found', status: 404 });
ctx.status = error.status;
ctx.body = error.body;
return;
}
throw error;
}
};

View file

@ -1,12 +1,11 @@
import { Application, ApplicationUpdate, Applications } from '@logto/schemas';
import { sql } from 'slonik';
import { sql, SlonikError } 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 RequestError from '@/errors/RequestError';
const { table, fields } = convertToIdentifiers(Applications);
@ -49,11 +48,6 @@ export const deleteApplicationById = async (id: string) => {
where id=${id}
`);
if (rowCount < 1) {
throw new RequestError({
code: 'entity.not_exists_with_id',
name: Applications.tableSingular,
id,
status: 404,
});
throw new SlonikError('Resource not found');
}
};

View file

@ -101,6 +101,7 @@ export default function applicationRoutes<T extends AuthedRouter>(router: T) {
async (ctx, next) => {
const { id } = ctx.guard.params;
// Note: will need delete cascade when application is joint with other tables
await findApplicationById(id);
await deleteApplicationById(id);
ctx.status = 204;
return next();

View file

@ -50,6 +50,7 @@ const errors = {
create_failed: 'Failed to create {{name}}.',
not_exists: 'The {{name}} does not exist.',
not_exists_with_id: 'The {{name}} with ID `{{id}}` does not exist.',
not_found: 'The resource does not exist',
},
};

View file

@ -52,6 +52,7 @@ const errors = {
create_failed: '创建 {{name}} 失败。',
not_exists: '该 {{name}} 不存在。',
not_exists_with_id: 'ID 为 `{{id}}` 的 {{name}} 不存在。',
not_found: '该资源不存在',
},
};