chore(data-source-main): i18n of field depended error (#4843)

This commit is contained in:
ChengLei Shao 2024-07-08 09:21:29 +08:00 committed by GitHub
parent 18801aa438
commit fb8ff62d01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 50 additions and 7 deletions

View File

@ -1,3 +1,4 @@
{ {
"field-name-exists": "Field name \"{{name}}\" already exists in collection \"{{collectionName}}\"" "field-name-exists": "Field name \"{{name}}\" already exists in collection \"{{collectionName}}\"",
"field-is-depended-on-by-other": "Can not delete field \"{{fieldName}}\" in \"{{fieldCollectionName}}\", it is used by field \"{{dependedFieldName}}\" in \"{{dependedFieldCollectionName}}\" as \"{{dependedFieldAs}}\""
} }

View File

@ -1,3 +1,4 @@
{ {
"field-name-exists": "字段标识 \"{{name}}\" 已存在" "field-name-exists": "字段标识 \"{{name}}\" 已存在",
"field-is-depended-on-by-other": "无法删除 \"{{fieldCollectionName}}\" 中的 \"{{fieldName}}\" 字段,它被 \"{{dependedFieldCollectionName}}\" 中的 \"{{dependedFieldName}}\" 字段用作 \"{{dependedFieldAs}}\""
} }

View File

@ -0,0 +1,16 @@
type FieldIsDependedOnByOtherErrorOptions = {
fieldName: string;
fieldCollectionName: string;
dependedFieldName: string;
dependedFieldCollectionName: string;
dependedFieldAs: string;
};
export class FieldIsDependedOnByOtherError extends Error {
constructor(public options: FieldIsDependedOnByOtherErrorOptions) {
super(
`Can't delete field ${options.fieldName} of ${options.fieldCollectionName}, it is used by field ${options.dependedFieldName} in collection ${options.dependedFieldCollectionName} as ${options.dependedFieldAs}`,
);
this.name = 'FieldIsDependedOnByOtherError';
}
}

View File

@ -1,4 +1,5 @@
import { Database } from '@nocobase/database'; import { Database } from '@nocobase/database';
import { FieldIsDependedOnByOtherError } from '../errors/field-is-depended-on-by-other';
export function beforeDestoryField(db: Database) { export function beforeDestoryField(db: Database) {
return async (model, opts) => { return async (model, opts) => {
@ -41,11 +42,13 @@ export function beforeDestoryField(db: Database) {
const usedAs = keys.find((key) => key.condition(field))['name']; const usedAs = keys.find((key) => key.condition(field))['name'];
throw new Error( throw new FieldIsDependedOnByOtherError({
`Can't delete field ${name} of ${collectionName}, it is used by field ${field.get( fieldName: name,
'name', fieldCollectionName: collectionName,
)} in collection ${field.get('collectionName')} as ${usedAs}`, dependedFieldName: field.get('name'),
); dependedFieldCollectionName: field.get('collectionName'),
dependedFieldAs: usedAs,
});
} }
}; };
} }

View File

@ -29,6 +29,7 @@ import collectionActions from './resourcers/collections';
import viewResourcer from './resourcers/views'; import viewResourcer from './resourcers/views';
import { FieldNameExistsError } from './errors/field-name-exists-error'; import { FieldNameExistsError } from './errors/field-name-exists-error';
import { beforeDestoryField } from './hooks/beforeDestoryField'; import { beforeDestoryField } from './hooks/beforeDestoryField';
import { FieldIsDependedOnByOtherError } from './errors/field-is-depended-on-by-other';
export class PluginDataSourceMainServer extends Plugin { export class PluginDataSourceMainServer extends Plugin {
public schema: string; public schema: string;
@ -335,6 +336,27 @@ export class PluginDataSourceMainServer extends Plugin {
}, },
); );
errorHandlerPlugin.errorHandler.register(
(err) => err instanceof FieldIsDependedOnByOtherError,
(err, ctx) => {
ctx.status = 400;
ctx.body = {
errors: [
{
message: ctx.i18n.t('field-is-depended-on-by-other', {
fieldName: err.options.fieldName,
fieldCollectionName: err.options.fieldCollectionName,
dependedFieldName: err.options.dependedFieldName,
dependedFieldCollectionName: err.options.dependedFieldCollectionName,
dependedFieldAs: err.options.dependedFieldAs,
ns: 'data-source-main',
}),
},
],
};
},
);
errorHandlerPlugin.errorHandler.register( errorHandlerPlugin.errorHandler.register(
(err) => err instanceof FieldNameExistsError, (err) => err instanceof FieldNameExistsError,
(err, ctx) => { (err, ctx) => {