Skip to content

Commit 12d0060

Browse files
authored
refactor!(NODE-3368): make name prop on error classes read-only (#2879)
1 parent d73c80c commit 12d0060

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

src/bulk/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,13 @@ export class MongoBulkWriteError extends MongoServerError {
684684
super(error as Error);
685685
Object.assign(this, error);
686686

687-
this.name = 'MongoBulkWriteError';
688687
this.result = result;
689688
}
690689

690+
get name(): string {
691+
return 'MongoBulkWriteError';
692+
}
693+
691694
/** Number of documents inserted. */
692695
get insertedCount(): number {
693696
return this.result.insertedCount;

src/cmap/errors.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ export class PoolClosedError extends MongoDriverError {
1212

1313
constructor(pool: ConnectionPool) {
1414
super('Attempted to check out a connection from closed connection pool');
15-
this.name = 'MongoPoolClosedError';
1615
this.address = pool.address;
1716
}
17+
18+
get name(): string {
19+
return 'MongoPoolClosedError';
20+
}
1821
}
1922

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

2831
constructor(pool: Connection | ConnectionPool) {
2932
super('Timed out while checking out a connection from connection pool');
30-
this.name = 'MongoWaitQueueTimeoutError';
3133
this.address = pool.address;
3234
}
35+
36+
get name(): string {
37+
return 'MongoWaitQueueTimeoutError';
38+
}
3339
}

src/error.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ export class MongoError extends Error {
8888
} else {
8989
super(message);
9090
}
91+
}
9192

92-
this.name = 'MongoError';
93+
get name(): string {
94+
return 'MongoError';
9395
}
9496

9597
/** Legacy name for server error responses */
@@ -152,7 +154,10 @@ export class MongoServerError extends MongoError {
152154
(this as any)[name] = message[name];
153155
}
154156
}
155-
this.name = 'MongoServerError';
157+
}
158+
159+
get name(): string {
160+
return 'MongoServerError';
156161
}
157162
}
158163

@@ -166,7 +171,10 @@ export class MongoDriverError extends MongoError {
166171
code?: string;
167172
constructor(message: string) {
168173
super(message);
169-
this.name = 'MongoDriverError';
174+
}
175+
176+
get name(): string {
177+
return 'MongoDriverError';
170178
}
171179
}
172180

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

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

192199
if (options && typeof options.beforeHandshake === 'boolean') {
193200
this[kBeforeHandshake] = options.beforeHandshake;
194201
}
195202
}
203+
204+
get name(): string {
205+
return 'MongoNetworkError';
206+
}
196207
}
197208

198209
/** @public */
@@ -212,7 +223,10 @@ export interface MongoNetworkTimeoutErrorOptions {
212223
export class MongoNetworkTimeoutError extends MongoNetworkError {
213224
constructor(message: string, options?: MongoNetworkTimeoutErrorOptions) {
214225
super(message, options);
215-
this.name = 'MongoNetworkTimeoutError';
226+
}
227+
228+
get name(): string {
229+
return 'MongoNetworkTimeoutError';
216230
}
217231
}
218232

@@ -224,7 +238,10 @@ export class MongoNetworkTimeoutError extends MongoNetworkError {
224238
export class MongoParseError extends MongoDriverError {
225239
constructor(message: string) {
226240
super(message);
227-
this.name = 'MongoParseError';
241+
}
242+
243+
get name(): string {
244+
return 'MongoParseError';
228245
}
229246
}
230247

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

247-
this.name = 'MongoSystemError';
248264
if (reason) {
249265
this.reason = reason;
250266
}
251267
}
268+
269+
get name(): string {
270+
return 'MongoSystemError';
271+
}
252272
}
253273

254274
/**
@@ -259,7 +279,10 @@ export class MongoSystemError extends MongoError {
259279
export class MongoServerSelectionError extends MongoSystemError {
260280
constructor(message: string, reason: TopologyDescription) {
261281
super(message, reason);
262-
this.name = 'MongoServerSelectionError';
282+
}
283+
284+
get name(): string {
285+
return 'MongoServerSelectionError';
263286
}
264287
}
265288

@@ -291,12 +314,15 @@ export class MongoWriteConcernError extends MongoServerError {
291314
}
292315

293316
super(message);
294-
this.name = 'MongoWriteConcernError';
295317

296318
if (result != null) {
297319
this.result = makeWriteConcernResultObject(result);
298320
}
299321
}
322+
323+
get name(): string {
324+
return 'MongoWriteConcernError';
325+
}
300326
}
301327

302328
// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms

test/unit/errors.test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
12
'use strict';
23

34
const expect = require('chai').expect;
45
const { getSymbolFrom } = require('../tools/utils');
5-
const MongoNetworkError = require('../../src/error').MongoNetworkError;
6+
const { MongoNetworkError } = require('../../src/index');
7+
const {
8+
PoolClosedError: MongoPoolClosedError,
9+
WaitQueueTimeoutError: MongoWaitQueueTimeoutError
10+
} = require('../../src/cmap/errors');
611

7-
describe('MongoErrors', function () {
8-
describe('MongoNetworkError', function () {
12+
describe('MongoErrors', () => {
13+
// import errors as object
14+
let errorClasses = Object.fromEntries(
15+
Object.entries(require('../../src/index')).filter(([key]) => key.endsWith('Error'))
16+
);
17+
errorClasses = { ...errorClasses, MongoPoolClosedError, MongoWaitQueueTimeoutError };
18+
19+
for (const errorName in errorClasses) {
20+
describe(errorName, () => {
21+
it(`name should be read-only`, () => {
22+
// Dynamically create error class with message
23+
let error = new errorClasses[errorName]('generated by test');
24+
// expect name property to be class name
25+
expect(error).to.have.property('name', errorName);
26+
27+
try {
28+
error.name = 'renamed by test';
29+
} catch (err) {}
30+
expect(error).to.have.property('name', errorName);
31+
});
32+
});
33+
}
34+
35+
describe('when MongoNetworkError is constructed', () => {
936
it('should only define beforeHandshake symbol if boolean option passed in', function () {
1037
const errorWithOptionTrue = new MongoNetworkError('', { beforeHandshake: true });
1138
expect(getSymbolFrom(errorWithOptionTrue, 'beforeHandshake', false)).to.be.a('symbol');

0 commit comments

Comments
 (0)