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.
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import type { ErrorWithContextWrapper } from '@/application/Parser/ContextualError';
|
|
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
|
import { formatAssertionMessage } from '@tests/shared/FormatAssertionMessage';
|
|
import { indentText } from '@tests/shared/Text';
|
|
import { ErrorWrapperStub } from '@tests/unit/shared/Stubs/ErrorWrapperStub';
|
|
|
|
interface ContextualErrorTestScenario {
|
|
readonly throwingAction: (wrapError: ErrorWithContextWrapper) => void;
|
|
readonly expectedWrappedError: Error;
|
|
readonly expectedContextMessage: string;
|
|
}
|
|
|
|
export function itThrowsContextualError(
|
|
testScenario: ContextualErrorTestScenario,
|
|
) {
|
|
it('throws wrapped error', () => {
|
|
// arrange
|
|
const expectedError = new Error();
|
|
const wrapperStub = new ErrorWrapperStub()
|
|
.withError(expectedError);
|
|
// act
|
|
const act = () => testScenario.throwingAction(wrapperStub.get());
|
|
// assert
|
|
expect(act).to.throw(expectedError);
|
|
});
|
|
it('wraps internal error', () => {
|
|
// arrange
|
|
const expectedInternalError = testScenario.expectedWrappedError;
|
|
const wrapperStub = new ErrorWrapperStub();
|
|
// act
|
|
try {
|
|
testScenario.throwingAction(wrapperStub.get());
|
|
} catch { /* Swallow */ }
|
|
// assert
|
|
expect(wrapperStub.lastError).to.deep.equal(expectedInternalError);
|
|
});
|
|
it('includes expected context', () => {
|
|
// arrange
|
|
const { expectedContextMessage: expectedContext } = testScenario;
|
|
const wrapperStub = new ErrorWrapperStub();
|
|
// act
|
|
try {
|
|
testScenario.throwingAction(wrapperStub.get());
|
|
} catch { /* Swallow */ }
|
|
// assert
|
|
expectExists(wrapperStub.lastContext);
|
|
expect(wrapperStub.lastContext).to.equal(expectedContext, formatAssertionMessage([
|
|
'Unexpected additional context (additional message added to the wrapped error).',
|
|
`Actual additional context:\n${indentText(wrapperStub.lastContext)}`,
|
|
`Expected additional context:\n${indentText(expectedContext)}`,
|
|
]));
|
|
});
|
|
}
|