Files
privacy.sexy/tests/unit/shared/Stubs/ScriptParserStub.ts
undergroundwires 4212c7b9e0 Improve context for errors thrown by compiler
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.
2024-05-25 13:55:30 +02:00

38 lines
1.2 KiB
TypeScript

import type { ScriptParser } from '@/application/Parser/Script/ScriptParser';
import type { IScript } from '@/domain/IScript';
import type { ScriptData } from '@/application/collections/';
import { ScriptStub } from './ScriptStub';
export class ScriptParserStub {
private readonly parsedScripts = new Map<IScript, Parameters<ScriptParser>>();
private readonly setupScripts = new Map<ScriptData, IScript>();
public get(): ScriptParser {
return (...parameters) => {
const [scriptData] = parameters;
const script = this.setupScripts.get(scriptData)
?? new ScriptStub(
`[${ScriptParserStub.name}] parsed script stub number ${this.parsedScripts.size + 1}`,
);
this.parsedScripts.set(script, parameters);
return script;
};
}
public getParseParameters(
script: IScript,
): Parameters<ScriptParser> {
const parameters = this.parsedScripts.get(script);
if (!parameters) {
throw new Error('Script has never been parsed.');
}
return parameters;
}
public setupParsedResultForData(scriptData: ScriptData, parsedResult: IScript): this {
this.setupScripts.set(scriptData, parsedResult);
return this;
}
}