This commit introduces a custom error object to provide additional context for errors throwing during parsing and compiling operations, improving troubleshooting. By integrating error context handling, the error messages become more informative and user-friendly, providing sequence of trace with context to aid in troubleshooting. Changes include: - Introduce custom error object that extends errors with contextual information. This replaces previous usages of `AggregateError` which is not displayed well by browsers when logged. - Improve parsing functions to encapsulate error context with more details. - Increase unit test coverage and refactor the related code to be more testable.
57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import type { CompiledCode } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/CompiledCode';
|
|
import type { FunctionCallCompiler } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/FunctionCallCompiler';
|
|
import type { ISharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/ISharedFunctionCollection';
|
|
import type { FunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/FunctionCall';
|
|
import { CompiledCodeStub } from './CompiledCodeStub';
|
|
|
|
interface FunctionCallCompilationTestScenario {
|
|
readonly calls: FunctionCall[];
|
|
readonly functions: ISharedFunctionCollection;
|
|
readonly result: CompiledCode;
|
|
}
|
|
|
|
export class FunctionCallCompilerStub implements FunctionCallCompiler {
|
|
public scenarios = new Array<FunctionCallCompilationTestScenario>();
|
|
|
|
private defaultCompiledCode: CompiledCode = new CompiledCodeStub()
|
|
.withCode(`[${FunctionCallCompilerStub.name}] function code`)
|
|
.withRevertCode(`[${FunctionCallCompilerStub.name}] function revert code`);
|
|
|
|
public setup(
|
|
calls: FunctionCall[],
|
|
functions: ISharedFunctionCollection,
|
|
result: CompiledCode,
|
|
): this {
|
|
this.scenarios.push({ calls, functions, result });
|
|
return this;
|
|
}
|
|
|
|
public withDefaultCompiledCode(defaultCompiledCode: CompiledCode): this {
|
|
this.defaultCompiledCode = defaultCompiledCode;
|
|
return this;
|
|
}
|
|
|
|
public compileFunctionCalls(
|
|
calls: readonly FunctionCall[],
|
|
functions: ISharedFunctionCollection,
|
|
): CompiledCode {
|
|
const predefined = this.scenarios
|
|
.find((s) => areEqual(s.calls, calls) && s.functions === functions);
|
|
if (predefined) {
|
|
return predefined.result;
|
|
}
|
|
return this.defaultCompiledCode;
|
|
}
|
|
}
|
|
|
|
function areEqual(
|
|
first: readonly FunctionCall[],
|
|
second: readonly FunctionCall[],
|
|
) {
|
|
const comparer = (a: FunctionCall, b: FunctionCall) => a.functionName
|
|
.localeCompare(b.functionName);
|
|
const printSorted = (calls: readonly FunctionCall[]) => JSON
|
|
.stringify([...calls].sort(comparer));
|
|
return printSorted(first) === printSorted(second);
|
|
}
|