mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:48:45 +08:00
feat:增加formulaEvalErrorHandler&conditionComputeErrorHandler用于跳过amis解析
This commit is contained in:
parent
829a4fb84e
commit
0b49e4c89c
@ -4,7 +4,7 @@ import {
|
||||
resolveVariableAndFilter,
|
||||
resolveVariableAndFilterForAsync
|
||||
} from '../src/utils/tpl-builtin';
|
||||
import {setFormulaEvalHandler} from '../src/utils';
|
||||
import {setFormulaEvalErrorHandler} from '../src/utils';
|
||||
|
||||
const filters = [
|
||||
{
|
||||
@ -643,14 +643,14 @@ test(`compat:test2`, () => {
|
||||
expect(resolveVariable('obj.x', data)).toEqual(123);
|
||||
});
|
||||
|
||||
test('compat:formulaEvalHandler', async () => {
|
||||
setFormulaEvalHandler(
|
||||
test('compat:formulaEvalErrorHandler', async () => {
|
||||
setFormulaEvalErrorHandler(
|
||||
(
|
||||
path?: string,
|
||||
data: object = {},
|
||||
defaultFilter: string = '| html',
|
||||
fallbackValue = (value: any) => value,
|
||||
skipFormulaEvalHandler: boolean = false
|
||||
skipFormulaEvalErrorHandler: boolean = false
|
||||
) => {
|
||||
return Promise.resolve(1 * 2 * 2);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import {
|
||||
resolveCondition,
|
||||
guid,
|
||||
registerConditionComputer,
|
||||
setConditionComputeHandler
|
||||
setConditionComputeErrorHandler
|
||||
} from '../src/utils/';
|
||||
|
||||
const data = {
|
||||
@ -512,7 +512,7 @@ test(`condition register`, async () => {
|
||||
|
||||
test(`condition conditionComputeHander`, async () => {
|
||||
// 无法解析时,自定义解析逻辑
|
||||
setConditionComputeHandler(
|
||||
setConditionComputeErrorHandler(
|
||||
(conditions: any, data: any, defaultResult: boolean) => {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
@ -11,7 +11,8 @@ const conditionResolverMap: {
|
||||
[op: string]: (left: any, right: any, fieldType?: string) => boolean;
|
||||
} = {};
|
||||
const DEFAULT_RESULT = true;
|
||||
let conditionComputeHandler: (
|
||||
|
||||
let conditionComputeErrorHandler: (
|
||||
conditions: any,
|
||||
data: any,
|
||||
defaultResult: boolean
|
||||
@ -40,7 +41,7 @@ export async function resolveCondition(
|
||||
} catch (e) {
|
||||
// 如果函数未定义,则交给handler
|
||||
if (e.name === 'FormulaEvalError') {
|
||||
return await conditionComputeHandler?.(
|
||||
return await conditionComputeErrorHandler?.(
|
||||
conditions.children,
|
||||
conditions.conjunction,
|
||||
data
|
||||
@ -313,14 +314,14 @@ export function getConditionComputers() {
|
||||
return conditionResolverMap;
|
||||
}
|
||||
|
||||
export function setConditionComputeHandler(
|
||||
export function setConditionComputeErrorHandler(
|
||||
fn: (
|
||||
conditions: any,
|
||||
data: any,
|
||||
defaultResult: boolean
|
||||
) => boolean | Promise<boolean>
|
||||
) {
|
||||
conditionComputeHandler = fn;
|
||||
conditionComputeErrorHandler = fn;
|
||||
}
|
||||
|
||||
registerConditionComputer('greater', greaterFunc);
|
||||
|
@ -1,6 +1,6 @@
|
||||
import {AsyncEvaluator, parse} from 'amis-formula';
|
||||
|
||||
let formulaEvalHandler: (
|
||||
let formulaEvalErrorHandler: (
|
||||
path: string,
|
||||
data?: object,
|
||||
...args: any[]
|
||||
@ -10,10 +10,10 @@ let formulaEvalHandler: (
|
||||
* 设置自定义函数,functions中找不到处理的函数时执行
|
||||
* @param fn
|
||||
*/
|
||||
export function setFormulaEvalHandler(
|
||||
export function setFormulaEvalErrorHandler(
|
||||
fn: (path: string, data?: object, ...args: any[]) => any
|
||||
): void {
|
||||
formulaEvalHandler = fn;
|
||||
formulaEvalErrorHandler = fn;
|
||||
}
|
||||
|
||||
export const resolveVariableAndFilterForAsync = async (
|
||||
@ -21,7 +21,7 @@ export const resolveVariableAndFilterForAsync = async (
|
||||
data: object = {},
|
||||
defaultFilter: string = '| html',
|
||||
fallbackValue = (value: any) => value,
|
||||
skipFormulaEvalHandler: boolean = false
|
||||
skipFormulaEvalErrorHandler: boolean = false
|
||||
) => {
|
||||
if (!path || typeof path !== 'string') {
|
||||
return undefined;
|
||||
@ -43,9 +43,14 @@ export const resolveVariableAndFilterForAsync = async (
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
if (e.name === 'FormulaEvalError') {
|
||||
if (!skipFormulaEvalHandler) {
|
||||
if (!skipFormulaEvalErrorHandler) {
|
||||
// 无法解析时,执行handler自定义解析逻辑
|
||||
return formulaEvalHandler?.(path, data, defaultFilter, fallbackValue);
|
||||
return formulaEvalErrorHandler?.(
|
||||
path,
|
||||
data,
|
||||
defaultFilter,
|
||||
fallbackValue
|
||||
);
|
||||
}
|
||||
// 跳过自定义解析逻辑,则直接抛异常
|
||||
throw e;
|
||||
|
Loading…
Reference in New Issue
Block a user