0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

refactor: improved error code of buildInsertInto()

This commit is contained in:
Gao Sun 2021-08-18 11:54:05 +08:00
parent acc2031e82
commit 6f604f40c2
No known key found for this signature in database
GPG key ID: 0F0EFA2E36639F31
10 changed files with 20 additions and 8 deletions

View file

@ -78,7 +78,10 @@ export const buildInsertInto: BuildInsertInto = <Schema extends SchemaLike<strin
rows: [entry],
} = result;
assert(!returning || entry, new RequestError('application.create_failed'));
assert(
!returning || entry,
new RequestError({ code: 'entity.create_failed', name: rest.tableSingular })
);
return entry;
};
};

View file

@ -10,8 +10,12 @@ export default class RequestError extends Error {
data: unknown;
constructor(input: RequestErrorMetadata | LogtoErrorCode, data?: unknown) {
const { code, status = 400 } = typeof input === 'string' ? { code: input } : input;
const message = i18next.t<string, LogtoErrorI18nKey>(`errors:${code}`);
const {
code,
status = 400,
...interpolation
} = typeof input === 'string' ? { code: input } : input;
const message = i18next.t<string, LogtoErrorI18nKey>(`errors:${code}`, interpolation);
super(message);

View file

@ -37,8 +37,8 @@ const errors = {
swagger: {
invalid_zod_type: 'Invalid Zod type, please check route guard config.',
},
application: {
create_failed: 'Failed to create application.',
entity: {
create_failed: 'Failed to create {{name}}.',
},
};

View file

@ -39,8 +39,8 @@ const errors = {
swagger: {
invalid_zod_type: '无效的 Zod 类型,请检查路由 guard 配置。',
},
application: {
create_failed: '创建应用失败。',
entity: {
create_failed: '创建{{name}}失败。',
},
};

View file

@ -1,6 +1,6 @@
import { LogtoErrorCode } from '@logto/phrases';
export type RequestErrorMetadata = {
export type RequestErrorMetadata = Record<string, unknown> & {
code: LogtoErrorCode;
status?: number;
};

View file

@ -14,6 +14,7 @@ export type ApplicationDBEntry = {
export const Applications: GeneratedSchema<ApplicationDBEntry> = Object.freeze({
table: 'applications',
tableSingular: 'application',
fields: {
id: 'id',
name: 'name',

View file

@ -12,6 +12,7 @@ export type OidcModelInstanceDBEntry = {
export const OidcModelInstances: GeneratedSchema<OidcModelInstanceDBEntry> = Object.freeze({
table: 'oidc_model_instances',
tableSingular: 'oidc_model_instance',
fields: {
modelName: 'model_name',
id: 'id',

View file

@ -16,6 +16,7 @@ export type UserDBEntry = {
export const Users: GeneratedSchema<UserDBEntry> = Object.freeze({
table: 'users',
tableSingular: 'user',
fields: {
id: 'id',
username: 'username',

View file

@ -7,6 +7,7 @@ export type SchemaLike<Key extends string> = {
export type GeneratedSchema<Schema extends SchemaLike<string>> = keyof Schema extends string
? Readonly<{
table: string;
tableSingular: string;
fields: {
[key in keyof Schema]: string;
};

View file

@ -234,6 +234,7 @@ const generate = async () => {
pascalCase: true,
})}: GeneratedSchema<${databaseEntryType}> = Object.freeze({`,
` table: '${name}',`,
` tableSingular: '${pluralize(name, 1)}',`,
' fields: {',
...fields.map(({ name }) => ` ${camelcase(name)}: '${name}',`),
' },',