Skip to content

Commit

Permalink
fix(core): don't include stack in formatError per default
Browse files Browse the repository at this point in the history
fix(mongo): adjust to new error messages
  • Loading branch information
marcj committed Mar 12, 2024
1 parent b5fa0ad commit 1b603ee
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,22 +827,22 @@ export function forwardTypeArguments(x: any, y: any): void {
y.Ω = x.Ω;
}

export function formatError(error: any): string {
export function formatError(error: any, withStack: boolean = false): string {
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')}`;
return `${(withStack && error.stack) || `AggregateError: ${error.message}`}\nErrors:\n${error.errors.map((v: any) => formatError(v)).join('\n')}`;
}

if (error instanceof Error) {
let current: any = error.cause;
let errors: string[] = [error.stack || error.message || 'Error'];
let errors: string[] = [(withStack && 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;
if (withStack && error.stack) return error.stack;
return String(error);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/mongo/src/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class MongoConnectionPool {
await Promise.allSettled(promises);
}
} catch (error: any) {
throw new MongoConnectionError(`Failed to connect: ${formatError(error)}`);
throw new MongoConnectionError(`Failed to connect: ${formatError(error)}`, { cause: error });
}
}

Expand Down Expand Up @@ -516,7 +516,7 @@ export class MongoConnection {
this.socket.on('error', (error) => {
this.connectingPromise = undefined;
this.status = MongoConnectionStatus.disconnected;
reject(new MongoConnectionError(formatError(error.message)));
reject(new MongoConnectionError(formatError(error.message), { cause: error }));
});

if (this.socket.destroyed) {
Expand Down
4 changes: 2 additions & 2 deletions packages/mongo/tests/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ test('errors connect', async () => {
called = event;
});

await expect(() => database.query(Test).findOne()).rejects.toThrow('MongoConnectionError: getaddrinfo ENOTFOUND invalid');
await expect(() => database.query(Test).findOne()).rejects.toThrow('Failed to connect: getaddrinfo ENOTFOUND invalid');
await expect(() => database.query(Test).findOne()).rejects.toBeInstanceOf(MongoConnectionError);

assertDefined(called);
assertInstanceOf(called.error, MongoConnectionError);
expect(called.error.message).toBe('Failed to connect: MongoConnectionError: getaddrinfo ENOTFOUND invalid');
expect(called.error.message).toContain('Failed to connect: getaddrinfo ENOTFOUND invalid');
});

test('errors raw', async () => {
Expand Down

0 comments on commit 1b603ee

Please sign in to comment.