This commit introduces stricter type validation across the application to reject objects with unexpected properties, enhancing the robustness and predictability of data handling. Changes include: - Implement a common utility to validate object types. - Refactor across various parsers and data handlers to utilize the new validations. - Update error messages for better clarity and troubleshooting.
68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
import type { ErrorWithContextWrapper } from '@/application/Parser/Common/ContextualError';
|
|
|
|
export class ErrorWrapperStub {
|
|
private errorToReturn: Error | undefined;
|
|
|
|
private parameters?: Parameters<ErrorWithContextWrapper>;
|
|
|
|
public get lastError(): Error | undefined {
|
|
if (!this.parameters) {
|
|
return undefined;
|
|
}
|
|
return getError(this.parameters);
|
|
}
|
|
|
|
public get lastContext(): string | undefined {
|
|
if (!this.parameters) {
|
|
return undefined;
|
|
}
|
|
return getAdditionalContext(this.parameters);
|
|
}
|
|
|
|
public withError(error: Error): this {
|
|
this.errorToReturn = error;
|
|
return this;
|
|
}
|
|
|
|
public get(): ErrorWithContextWrapper {
|
|
return (...args) => {
|
|
this.parameters = args;
|
|
if (this.errorToReturn) {
|
|
return this.errorToReturn;
|
|
}
|
|
return new Error(
|
|
`[${ErrorWrapperStub.name}] Error wrapped with additional context.`
|
|
+ `\nAdditional context: ${getAdditionalContext(args)}`
|
|
+ `\nWrapped error message: ${getError(args).message}`
|
|
+ `\nWrapped error stack trace:\n${getLimitedStackTrace(getError(args), 5)}`,
|
|
);
|
|
};
|
|
}
|
|
}
|
|
|
|
function getAdditionalContext(
|
|
parameters: Parameters<ErrorWithContextWrapper>,
|
|
): string {
|
|
return parameters[1];
|
|
}
|
|
|
|
function getError(
|
|
parameters: Parameters<ErrorWithContextWrapper>,
|
|
): Error {
|
|
return parameters[0];
|
|
}
|
|
|
|
function getLimitedStackTrace(
|
|
error: Error,
|
|
limit: number,
|
|
): string {
|
|
const { stack } = error;
|
|
if (!stack) {
|
|
return 'No stack trace available';
|
|
}
|
|
return stack
|
|
.split('\n')
|
|
.slice(0, limit + 1)
|
|
.join('\n');
|
|
}
|