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.
This commit is contained in:
52
tests/unit/domain/ScriptCodeFactory.spec.ts
Normal file
52
tests/unit/domain/ScriptCodeFactory.spec.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { createScriptCode } from '@/domain/ScriptCodeFactory';
|
||||
|
||||
describe('ScriptCodeFactory', () => {
|
||||
describe('createScriptCode', () => {
|
||||
it('generates script code with given `code`', () => {
|
||||
// arrange
|
||||
const expectedCode = 'expected code';
|
||||
const context = new TestContext()
|
||||
.withCode(expectedCode);
|
||||
// act
|
||||
const code = context.createScriptCode();
|
||||
// assert
|
||||
const actualCode = code.execute;
|
||||
expect(actualCode).to.equal(expectedCode);
|
||||
});
|
||||
|
||||
it('generates script code with given `revertCode`', () => {
|
||||
// arrange
|
||||
const expectedRevertCode = 'expected revert code';
|
||||
const context = new TestContext()
|
||||
.withRevertCode(expectedRevertCode);
|
||||
// act
|
||||
const code = context.createScriptCode();
|
||||
// assert
|
||||
const actualRevertCode = code.revert;
|
||||
expect(actualRevertCode).to.equal(expectedRevertCode);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
private code = `[${TestContext}] code`;
|
||||
|
||||
private revertCode = `[${TestContext}] revertCode`;
|
||||
|
||||
public withCode(code: string): this {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withRevertCode(revertCode: string): this {
|
||||
this.revertCode = revertCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public createScriptCode(): ReturnType<typeof createScriptCode> {
|
||||
return createScriptCode(
|
||||
this.code,
|
||||
this.revertCode,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user