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:
57
tests/unit/shared/Stubs/NodeDataValidatorStub.ts
Normal file
57
tests/unit/shared/Stubs/NodeDataValidatorStub.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { NodeData } from '@/application/Parser/NodeValidation/NodeData';
|
||||
import type { NodeDataValidator, NodeDataValidatorFactory } from '@/application/Parser/NodeValidation/NodeDataValidator';
|
||||
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
|
||||
|
||||
export const createNodeDataValidatorFactoryStub
|
||||
: NodeDataValidatorFactory = () => new NodeDataValidatorStub();
|
||||
|
||||
export class NodeDataValidatorStub
|
||||
extends StubWithObservableMethodCalls<NodeDataValidator>
|
||||
implements NodeDataValidator {
|
||||
private assertThrowsOnFalseCondition = true;
|
||||
|
||||
public withAssertThrowsOnFalseCondition(enableAssertThrows: boolean): this {
|
||||
this.assertThrowsOnFalseCondition = enableAssertThrows;
|
||||
return this;
|
||||
}
|
||||
|
||||
public assertValidName(nameValue: string): this {
|
||||
this.registerMethodCall({
|
||||
methodName: 'assertValidName',
|
||||
args: [nameValue],
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public assertDefined(node: NodeData): this {
|
||||
this.registerMethodCall({
|
||||
methodName: 'assertDefined',
|
||||
args: [node],
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public assert(
|
||||
validationPredicate: () => boolean,
|
||||
errorMessage: string,
|
||||
): this {
|
||||
this.registerMethodCall({
|
||||
methodName: 'assert',
|
||||
args: [validationPredicate, errorMessage],
|
||||
});
|
||||
if (this.assertThrowsOnFalseCondition) {
|
||||
if (!validationPredicate()) {
|
||||
throw new Error(`[${NodeDataValidatorStub.name}] Assert validation failed: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public createContextualErrorMessage(errorMessage: string): string {
|
||||
this.registerMethodCall({
|
||||
methodName: 'createContextualErrorMessage',
|
||||
args: [errorMessage],
|
||||
});
|
||||
return `${NodeDataValidatorStub.name}: ${errorMessage}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user