Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5296): construct error messages for AggregateErrors in Node16+ #3682

Merged
merged 6 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
serverless?: 'forbid' | 'allow' | 'require';
auth?: 'enabled' | 'disabled';
idmsMockServer?: true;
nodejs?: string;
};

sessions?: {
Expand Down
23 changes: 20 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export interface ErrorDescription extends Document {
errInfo?: Document;
}

function isAggregateError(e: Error): e is Error & { errors: Error[] } {
return 'errors' in e && Array.isArray(e.errors);
}

/**
* @public
* @category Error
Expand All @@ -131,15 +135,28 @@ export class MongoError extends Error {
cause?: Error; // depending on the node version, this may or may not exist on the base

constructor(message: string | Error) {
super(MongoError.buildErrorMessage(message));
if (message instanceof Error) {
super(message.message);
this.cause = message;
} else {
super(message);
}

this[kErrorLabels] = new Set();
}

/** @internal */
private static buildErrorMessage(e: Error | string): string {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
if (typeof e === 'string') {
return e;
}
if (isAggregateError(e) && e.message.length === 0) {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
return e.errors.length === 0
? 'AggregateError has an empty errors array. Please check the `cause` property for more information.'
: e.errors.map(({ message }) => message).join(', ');
}

return e.message;
}

override get name(): string {
return 'MongoError';
}
Expand Down
54 changes: 54 additions & 0 deletions test/integration/node-specific/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from 'chai';

import { MongoClient, MongoError, MongoServerSelectionError } from '../../mongodb';

describe('Error (Integration)', function () {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
describe('AggregateErrors', function () {
for (const { errors, message } of [
{
errors: [],
message:
'AggregateError has an empty errors array. Please check the `cause` property for more information.'
},
{ errors: [new Error('message 1')], message: 'message 1' },
{
errors: [new Error('message 1'), new Error('message 2')],
message: 'message 1, message 2'
}
]) {
it(
`constructs the message properly with an array of ${errors.length} errors`,
{ requires: { nodejs: '>=16' } },
() => {
const error = new AggregateError(errors);
const mongoError = new MongoError(error);

expect(mongoError.message).to.equal(message);
}
);
}

context('when the message on the AggregateError is non-empty', () => {
it(`uses the AggregateError's message`, { requires: { nodejs: '>=16' } }, () => {
const error = new AggregateError([new Error('non-empty')]);
error.message = 'custom error message';
const mongoError = new MongoError(error);
expect(mongoError.message).to.equal('custom error message');
});
});

it('sets the AggregateError to the cause property', { requires: { nodejs: '>=16' } }, () => {
const error = new AggregateError([new Error('error 1')]);
const mongoError = new MongoError(error);
expect(mongoError.cause).to.equal(error);
});
});

it('NODE-5296: handles aggregate errors from dns lookup', async function () {
const error = await MongoClient.connect('mongodb://localhost:27222', {
serverSelectionTimeoutMS: 1000
}).catch(e => e);
expect(error).to.be.instanceOf(MongoServerSelectionError);
expect(error.message).not.to.be.empty;
});
});
26 changes: 26 additions & 0 deletions test/tools/runner/filters/node_version_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { satisfies } = require('semver');

/**
* Filter for specific nodejs versions
*
* example:
* metadata: {
* requires: {
* nodejs: '>=14'
* }
* }
*/
class NodeVersionFilter {
filter(test) {
const nodeVersionRange = test?.metadata?.requires?.nodejs;
if (!nodeVersionRange) {
return true;
}

return satisfies(process.version, nodeVersionRange);
}
}

module.exports = NodeVersionFilter;