mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
chore(data-source-main): i18n of field depended error (#4843)
This commit is contained in:
parent
18801aa438
commit
fb8ff62d01
@ -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}}\""
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"field-name-exists": "字段标识 \"{{name}}\" 已存在"
|
"field-name-exists": "字段标识 \"{{name}}\" 已存在",
|
||||||
|
"field-is-depended-on-by-other": "无法删除 \"{{fieldCollectionName}}\" 中的 \"{{fieldName}}\" 字段,它被 \"{{dependedFieldCollectionName}}\" 中的 \"{{dependedFieldName}}\" 字段用作 \"{{dependedFieldAs}}\""
|
||||||
}
|
}
|
||||||
|
@ -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';
|
||||||
|
}
|
||||||
|
}
|
@ -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,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user