Skip to content

Commit

Permalink
refactor!(NODE-3368): make name prop on error classes read-only (#2879)
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina authored and ljhaywar committed Nov 9, 2021
1 parent 610dd3c commit 512c360
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,13 @@ export class MongoBulkWriteError extends MongoServerError {
super(error as Error);
Object.assign(this, error);

this.name = 'MongoBulkWriteError';
this.result = result;
}

get name(): string {
return 'MongoBulkWriteError';
}

/** Number of documents inserted. */
get insertedCount(): number {
return this.result.insertedCount;
Expand Down
10 changes: 8 additions & 2 deletions src/cmap/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export class PoolClosedError extends MongoDriverError {

constructor(pool: ConnectionPool) {
super('Attempted to check out a connection from closed connection pool');
this.name = 'MongoPoolClosedError';
this.address = pool.address;
}

get name(): string {
return 'MongoPoolClosedError';
}
}

/**
Expand All @@ -27,7 +30,10 @@ export class WaitQueueTimeoutError extends MongoDriverError {

constructor(pool: Connection | ConnectionPool) {
super('Timed out while checking out a connection from connection pool');
this.name = 'MongoWaitQueueTimeoutError';
this.address = pool.address;
}

get name(): string {
return 'MongoWaitQueueTimeoutError';
}
}
44 changes: 35 additions & 9 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ export class MongoError extends Error {
} else {
super(message);
}
}

this.name = 'MongoError';
get name(): string {
return 'MongoError';
}

/** Legacy name for server error responses */
Expand Down Expand Up @@ -152,7 +154,10 @@ export class MongoServerError extends MongoError {
(this as any)[name] = message[name];
}
}
this.name = 'MongoServerError';
}

get name(): string {
return 'MongoServerError';
}
}

Expand All @@ -166,7 +171,10 @@ export class MongoDriverError extends MongoError {
code?: string;
constructor(message: string) {
super(message);
this.name = 'MongoDriverError';
}

get name(): string {
return 'MongoDriverError';
}
}

Expand All @@ -187,12 +195,15 @@ export class MongoNetworkError extends MongoError {

constructor(message: string | Error, options?: { beforeHandshake?: boolean }) {
super(message);
this.name = 'MongoNetworkError';

if (options && typeof options.beforeHandshake === 'boolean') {
this[kBeforeHandshake] = options.beforeHandshake;
}
}

get name(): string {
return 'MongoNetworkError';
}
}

/** @public */
Expand All @@ -212,7 +223,10 @@ export interface MongoNetworkTimeoutErrorOptions {
export class MongoNetworkTimeoutError extends MongoNetworkError {
constructor(message: string, options?: MongoNetworkTimeoutErrorOptions) {
super(message, options);
this.name = 'MongoNetworkTimeoutError';
}

get name(): string {
return 'MongoNetworkTimeoutError';
}
}

Expand All @@ -224,7 +238,10 @@ export class MongoNetworkTimeoutError extends MongoNetworkError {
export class MongoParseError extends MongoDriverError {
constructor(message: string) {
super(message);
this.name = 'MongoParseError';
}

get name(): string {
return 'MongoParseError';
}
}

Expand All @@ -244,11 +261,14 @@ export class MongoSystemError extends MongoError {
super(message);
}

this.name = 'MongoSystemError';
if (reason) {
this.reason = reason;
}
}

get name(): string {
return 'MongoSystemError';
}
}

/**
Expand All @@ -259,7 +279,10 @@ export class MongoSystemError extends MongoError {
export class MongoServerSelectionError extends MongoSystemError {
constructor(message: string, reason: TopologyDescription) {
super(message, reason);
this.name = 'MongoServerSelectionError';
}

get name(): string {
return 'MongoServerSelectionError';
}
}

Expand Down Expand Up @@ -291,12 +314,15 @@ export class MongoWriteConcernError extends MongoServerError {
}

super(message);
this.name = 'MongoWriteConcernError';

if (result != null) {
this.result = makeWriteConcernResultObject(result);
}
}

get name(): string {
return 'MongoWriteConcernError';
}
}

// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
Expand Down
33 changes: 30 additions & 3 deletions test/unit/errors.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
'use strict';

const expect = require('chai').expect;
const { getSymbolFrom } = require('../tools/utils');
const MongoNetworkError = require('../../src/error').MongoNetworkError;
const { MongoNetworkError } = require('../../src/index');
const {
PoolClosedError: MongoPoolClosedError,
WaitQueueTimeoutError: MongoWaitQueueTimeoutError
} = require('../../src/cmap/errors');

describe('MongoErrors', function () {
describe('MongoNetworkError', function () {
describe('MongoErrors', () => {
// import errors as object
let errorClasses = Object.fromEntries(
Object.entries(require('../../src/index')).filter(([key]) => key.endsWith('Error'))
);
errorClasses = { ...errorClasses, MongoPoolClosedError, MongoWaitQueueTimeoutError };

for (const errorName in errorClasses) {
describe(errorName, () => {
it(`name should be read-only`, () => {
// Dynamically create error class with message
let error = new errorClasses[errorName]('generated by test');
// expect name property to be class name
expect(error).to.have.property('name', errorName);

try {
error.name = 'renamed by test';
} catch (err) {}
expect(error).to.have.property('name', errorName);
});
});
}

describe('when MongoNetworkError is constructed', () => {
it('should only define beforeHandshake symbol if boolean option passed in', function () {
const errorWithOptionTrue = new MongoNetworkError('', { beforeHandshake: true });
expect(getSymbolFrom(errorWithOptionTrue, 'beforeHandshake', false)).to.be.a('symbol');
Expand Down

0 comments on commit 512c360

Please sign in to comment.