ChengLei Shao 61e7a89067
refactor: export action (#5665)
* chore: export xlsx command

* chore: container service

* chore: report export progress

* chore: export

* chore: performInsert

* chore: export command

* chore: base exporter

* chore: limit option

* chore: export

* chore: export

* chore: types

* chore: ws socket with auth

* refactor: refactor exporter, change addRows method to handleRow method to support row-by-row processing

* chore: xlsx exporter

* chore: base exporter

* chore: export limit

* chore: import action

* chore: load websocket

* chore: import action

* chore: object to cli args

* chore: import options

* chore: import action

* chore: import runner

* chore: import action

* chore: import options

* chore: import options

* chore: plugin load event

* chore: test

* chore: i18n

* chore: i18n

* chore: i18n

* fix: ws auth status

* chore: cache in data source

* chore: load datasource

* chore: import with field alias

* fix: build

* fix: test

* fix: build

* fix: import schema settings

* fix: import action

* fix: import progress

* chore: template creator

* fix: import error message

* fix: import error message

* chore: test

* chore: workflow dispatch event

* chore: export alias

* fix: typo

* chore: error render

* fix: event error

* chore: send message to tags

* fix: test

* fix: import action setting
2024-12-31 20:16:03 +08:00

73 lines
2.2 KiB
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { Schema } from '@formily/json-schema';
import { BaseError } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import lodash from 'lodash';
import { ErrorHandler } from './error-handler';
export class PluginErrorHandlerServer extends Plugin {
errorHandler: ErrorHandler = new ErrorHandler();
i18nNs = 'error-handler';
beforeLoad() {
this.registerSequelizeValidationErrorHandler();
}
registerSequelizeValidationErrorHandler() {
const findFieldTitle = (instance, path, tFunc, ctx) => {
if (!instance) {
return path;
}
const model = instance.constructor;
const dataSourceKey = ctx.get('x-data-source');
const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
const database = dataSource ? dataSource.collectionManager.db : ctx.db;
const collection = database.modelCollection.get(model);
if (!collection) {
return path;
}
const field = collection.getField(path);
const fieldOptions = Schema.compile(field?.options, { t: tFunc });
const title = lodash.get(fieldOptions, 'uiSchema.title', path);
return title;
};
this.errorHandler.register(
(err) =>
err?.errors?.length &&
(err.name === 'SequelizeValidationError' || err.name === 'SequelizeUniqueConstraintError'),
(err, ctx) => {
ctx.body = {
errors: err.errors.map((err) => {
const t = ctx.i18n.t;
const title = findFieldTitle(err.instance, err.path, t, ctx);
return {
message: t(err.type, {
ns: this.i18nNs,
field: t(title, { ns: ['lm-collections', 'client'] }),
}),
};
}),
};
ctx.status = 400;
},
);
}
async load() {
this.app.use(this.errorHandler.middleware(), { after: 'i18n', tag: 'errorHandler', before: 'cors' });
}
}