Skip to content

Commit

Permalink
fix(type): print Error cause chain in formatError
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Mar 10, 2024
1 parent c0d3c76 commit c2a413a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export function isNumeric(s: string | number): boolean {
return true;
}

export const isInteger: (obj: any) => obj is number = Number.isInteger as any || function (obj: any) {
export const isInteger: (obj: any) => obj is number = Number.isInteger as any || function(obj: any) {
return (obj % 1) === 0;
};

Expand Down Expand Up @@ -828,21 +828,28 @@ export function forwardTypeArguments(x: any, y: any): void {
}

export function formatError(error: any): string {
if (error instanceof AggregateError) {
return error.errors.map(v => formatError(v)).join('\n');
if (error && error.name === 'AggregateError' && 'errors' in error) {
return `${error.stack || `AggregateError: ${error.message}`}\nErrors:\n${error.errors.map((v: any) => formatError(v)).join('\n')}`;
}

if (error instanceof Error) {
return `${getClassName(error)}: ${error.message}`;
let current: any = error.cause;
let errors: string[] = [error.stack || error.message || 'Error'];
while (current) {
errors.push(`cause by ${formatError(current)}`);
current = current.cause;
}
return errors.join('\n');
}

if (error.stack) return error.stack;
return String(error);
}

/**
* Asserts that the given object is an instance of the given class.
*/
export function assertInstanceOf<T>(object: any, constructor: { new (...args: any[]): T }): asserts object is T {
export function assertInstanceOf<T>(object: any, constructor: { new(...args: any[]): T }): asserts object is T {
if (!(object instanceof constructor)) {
throw new Error(`Object ${getClassName(object)} is not an instance of the expected class ${getClassName(constructor)}`);
}
Expand Down

0 comments on commit c2a413a

Please sign in to comment.