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:
parent
acc2031e82
commit
6f604f40c2
10 changed files with 20 additions and 8 deletions
|
@ -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;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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}}.',
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -39,8 +39,8 @@ const errors = {
|
|||
swagger: {
|
||||
invalid_zod_type: '无效的 Zod 类型,请检查路由 guard 配置。',
|
||||
},
|
||||
application: {
|
||||
create_failed: '创建应用失败。',
|
||||
entity: {
|
||||
create_failed: '创建{{name}}失败。',
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { LogtoErrorCode } from '@logto/phrases';
|
||||
|
||||
export type RequestErrorMetadata = {
|
||||
export type RequestErrorMetadata = Record<string, unknown> & {
|
||||
code: LogtoErrorCode;
|
||||
status?: number;
|
||||
};
|
||||
|
|
|
@ -14,6 +14,7 @@ export type ApplicationDBEntry = {
|
|||
|
||||
export const Applications: GeneratedSchema<ApplicationDBEntry> = Object.freeze({
|
||||
table: 'applications',
|
||||
tableSingular: 'application',
|
||||
fields: {
|
||||
id: 'id',
|
||||
name: 'name',
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -16,6 +16,7 @@ export type UserDBEntry = {
|
|||
|
||||
export const Users: GeneratedSchema<UserDBEntry> = Object.freeze({
|
||||
table: 'users',
|
||||
tableSingular: 'user',
|
||||
fields: {
|
||||
id: 'id',
|
||||
username: 'username',
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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}',`),
|
||||
' },',
|
||||
|
|
Loading…
Add table
Reference in a new issue