-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
error.test.ts
642 lines (579 loc) · 23.1 KB
/
error.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import { expect } from 'chai';
import { setTimeout } from 'timers';
import {
PoolClosedError as MongoPoolClosedError,
WaitQueueTimeoutError as MongoWaitQueueTimeoutError
} from '../../src/cmap/errors';
import {
isResumableError,
isRetryableReadError,
isSDAMUnrecoverableError,
LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE,
LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE,
MONGODB_ERROR_CODES,
MongoErrorLabel,
MongoNetworkTimeoutError,
MongoSystemError,
needsRetryableWriteLabel,
NODE_IS_RECOVERING_ERROR_MESSAGE
} from '../../src/error';
import * as importsFromErrorSrc from '../../src/error';
import {
MongoError,
MongoNetworkError,
MongoParseError,
MongoServerError,
MongoWriteConcernError,
TopologyDescription
} from '../../src/index';
import * as importsFromEntryPoint from '../../src/index';
import { Topology, TopologyOptions } from '../../src/sdam/topology';
import { isHello, ns, setDifference } from '../../src/utils';
import { ReplSetFixture } from '../tools/common';
import { cleanup } from '../tools/mongodb-mock/index';
import { getSymbolFrom } from '../tools/utils';
describe('MongoErrors', () => {
let errorClassesFromEntryPoint = Object.fromEntries(
Object.entries(importsFromEntryPoint).filter(
([key, value]) => key.endsWith('Error') && value.toString().startsWith('class')
)
) as any;
errorClassesFromEntryPoint = {
...errorClassesFromEntryPoint,
MongoPoolClosedError,
MongoWaitQueueTimeoutError
};
const errorClassesFromErrorSrc = Object.fromEntries(
Object.entries(importsFromErrorSrc).filter(
([key, value]) => key.endsWith('Error') && value.toString().startsWith('class')
)
);
it('all defined errors should be public', () => {
expect(
setDifference(Object.keys(errorClassesFromEntryPoint), Object.keys(errorClassesFromErrorSrc))
).to.have.property('size', 3);
expect(
setDifference(Object.keys(errorClassesFromErrorSrc), Object.keys(errorClassesFromEntryPoint))
).to.have.property('size', 0);
});
describe('error names should be read-only', () => {
for (const [errorName, errorClass] of Object.entries(errorClassesFromEntryPoint)) {
it(`${errorName} should be read-only`, () => {
// Dynamically create error class with message
const error = new (errorClass as any)('generated by test', {});
// expect name property to be class name
expect(error).to.have.property('name', errorName);
try {
error.name = 'renamed by test';
// eslint-disable-next-line no-empty
} catch (err) {}
expect(error).to.have.property('name', errorName);
});
}
});
describe('MongoError#constructor', () => {
it('should accept a string', function () {
const errorMessage = 'A test error';
const err = new MongoError(errorMessage);
expect(err).to.be.an.instanceof(Error);
expect(err.name).to.equal('MongoError');
expect(err.message).to.equal(errorMessage);
});
it('should accept an Error object', () => {
const errorMessage = 'A test error';
const err = new MongoError(new Error(errorMessage));
expect(err).to.be.an.instanceof(Error);
expect(err.name).to.equal('MongoError');
expect(err.message).to.equal(errorMessage);
});
});
describe('MongoServerError#constructor', () => {
it('should accept an object', function () {
const errorMessage = 'A test error';
const err = new MongoServerError({ message: errorMessage, someData: 12345 });
expect(err).to.be.an.instanceof(Error);
expect(err.name).to.equal('MongoServerError');
expect(err.message).to.equal(errorMessage);
expect(err.someData).to.equal(12345);
});
});
describe('MongoNetworkError#constructor', () => {
it('should accept a string', function () {
const errorMessage = 'A test error';
const err = new MongoNetworkError(errorMessage);
expect(err).to.be.an.instanceof(Error);
expect(err).to.be.an.instanceof(MongoError);
expect(err.name).to.equal('MongoNetworkError');
expect(err.message).to.equal(errorMessage);
});
});
describe('MongoSystemError#constructor', () => {
context('when the topology description contains an error code', () => {
it('contains the specified code as a top level property', () => {
const topologyDescription = {
error: {
code: 123
}
} as TopologyDescription;
const error = new MongoSystemError('something went wrong', topologyDescription);
expect(error).to.haveOwnProperty('code', 123);
});
});
context('when the topology description does not contain an error code', () => {
it('contains the code as a top level property that is undefined', () => {
const topologyDescription = { error: {} } as TopologyDescription;
const error = new MongoSystemError('something went wrong', topologyDescription);
expect(error).to.haveOwnProperty('code', undefined);
});
});
context('when the topology description does not contain an error property', () => {
it('contains the code as a top level property that is undefined', () => {
const topologyDescription = {} as TopologyDescription;
const error = new MongoSystemError('something went wrong', topologyDescription);
expect(error).to.haveOwnProperty('code', undefined);
});
});
});
describe('#isSDAMUnrecoverableError', function () {
context('when the error is a MongoParseError', function () {
it('returns true', function () {
const error = new MongoParseError('');
expect(isSDAMUnrecoverableError(error)).to.be.true;
});
});
context('when the error is null', function () {
it('returns true', function () {
expect(isSDAMUnrecoverableError(null)).to.be.true;
});
});
context('when the error has a "node is recovering" error code', function () {
it('returns true', function () {
const error = new MongoError('');
// Code for NotPrimaryOrSecondary
error.code = 13436;
expect(isSDAMUnrecoverableError(error)).to.be.true;
});
});
context('when the error has a "not writable primary" error code', function () {
it('returns true', function () {
const error = new MongoError('');
// Code for NotWritablePrimary
error.code = 10107;
expect(isSDAMUnrecoverableError(error)).to.be.true;
});
});
context(
'when the code is not a "node is recovering" error and not a "not writable primary" error',
function () {
it('returns false', function () {
// If the response includes an error code, it MUST be solely used to determine if error is a "node is recovering" or "not writable primary" error.
const error = new MongoError(NODE_IS_RECOVERING_ERROR_MESSAGE.source);
error.code = 555;
expect(isSDAMUnrecoverableError(error)).to.be.false;
});
}
);
context(
'when the error message contains the legacy "not primary" message and no error code is used',
function () {
it('returns true', function () {
const error = new MongoError(
`this is ${LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.source}.`
);
expect(isSDAMUnrecoverableError(error)).to.be.true;
});
}
);
context(
'when the error message contains "node is recovering" and no error code is used',
function () {
it('returns true', function () {
const error = new MongoError(`the ${NODE_IS_RECOVERING_ERROR_MESSAGE} from an error`);
expect(isSDAMUnrecoverableError(error)).to.be.true;
});
}
);
context(
'when the error message contains the legacy "not primary or secondary" message and no error code is used',
function () {
it('returns true', function () {
const error = new MongoError(
`this is ${LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE}, so we have a problem `
);
expect(isSDAMUnrecoverableError(error)).to.be.true;
});
}
);
});
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');
const errorWithOptionFalse = new MongoNetworkError('', { beforeHandshake: false });
expect(getSymbolFrom(errorWithOptionFalse, 'beforeHandshake', false)).to.be.a('symbol');
const errorWithBadOption = new MongoNetworkError('', {
// @ts-expect-error: beforeHandshake must be a boolean value
beforeHandshake: 'not boolean'
});
expect(getSymbolFrom(errorWithBadOption, 'beforeHandshake', false)).to.be.an('undefined');
const errorWithoutOption = new MongoNetworkError('');
expect(getSymbolFrom(errorWithoutOption, 'beforeHandshake', false)).to.be.an('undefined');
});
});
describe('WriteConcernError', function () {
let test;
const RAW_USER_WRITE_CONCERN_CMD = {
createUser: 'foo2',
pwd: 'pwd',
roles: ['read'],
writeConcern: { w: 'majority', wtimeoutMS: 1 }
};
const RAW_USER_WRITE_CONCERN_ERROR = {
ok: 0,
errmsg: 'waiting for replication timed out',
code: 64,
codeName: 'WriteConcernFailed',
writeConcernError: {
code: 64,
codeName: 'WriteConcernFailed',
errmsg: 'waiting for replication timed out',
errInfo: {
wtimeout: true
}
}
};
const RAW_USER_WRITE_CONCERN_ERROR_INFO = {
ok: 0,
errmsg: 'waiting for replication timed out',
code: 64,
codeName: 'WriteConcernFailed',
writeConcernError: {
code: 64,
codeName: 'WriteConcernFailed',
errmsg: 'waiting for replication timed out',
errInfo: {
writeConcern: {
w: 2,
wtimeout: 0,
provenance: 'clientSupplied'
}
}
}
};
before(() => (test = new ReplSetFixture()));
afterEach(() => cleanup());
beforeEach(() => test.setup());
function makeAndConnectReplSet(cb) {
let invoked = false;
const replSet = new Topology(
[test.primaryServer.hostAddress(), test.firstSecondaryServer.hostAddress()],
{ replicaSet: 'rs' } as TopologyOptions
);
replSet.once('error', err => {
if (invoked) {
return;
}
invoked = true;
cb(err);
});
replSet.on('connect', () => {
if (invoked) {
return;
}
invoked = true;
cb(undefined, replSet);
});
replSet.connect();
}
it('should expose a user command writeConcern error like a normal WriteConcernError', function (done) {
test.primaryServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
setTimeout(() => request.reply(test.primaryStates[0]));
} else if (doc.createUser) {
setTimeout(() => request.reply(RAW_USER_WRITE_CONCERN_ERROR));
}
});
makeAndConnectReplSet((err, topology) => {
// cleanup the server before calling done
const cleanup = err => topology.close({ force: true }, err2 => done(err || err2));
if (err) {
return cleanup(err);
}
topology.selectServer('primary', {}, (err, server) => {
expect(err).to.not.exist;
server.command(ns('db1'), Object.assign({}, RAW_USER_WRITE_CONCERN_CMD), {}, err => {
let _err;
try {
expect(err).to.be.an.instanceOf(MongoWriteConcernError);
expect(err.result).to.exist;
expect(err.result).to.have.property('ok', 1);
expect(err.result).to.not.have.property('errmsg');
expect(err.result).to.not.have.property('code');
expect(err.result).to.not.have.property('codeName');
expect(err.result).to.have.property('writeConcernError');
} catch (e) {
_err = e;
} finally {
cleanup(_err);
}
});
});
});
});
it('should propagate writeConcernError.errInfo ', function (done) {
test.primaryServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
setTimeout(() => request.reply(test.primaryStates[0]));
} else if (doc.createUser) {
setTimeout(() => request.reply(RAW_USER_WRITE_CONCERN_ERROR_INFO));
}
});
makeAndConnectReplSet((err, topology) => {
// cleanup the server before calling done
const cleanup = err => topology.close(err2 => done(err || err2));
if (err) {
return cleanup(err);
}
topology.selectServer('primary', {}, (err, server) => {
expect(err).to.not.exist;
server.command(ns('db1'), Object.assign({}, RAW_USER_WRITE_CONCERN_CMD), {}, err => {
let _err;
try {
expect(err).to.be.an.instanceOf(MongoWriteConcernError);
expect(err.result).to.exist;
expect(err.result.writeConcernError).to.deep.equal(
RAW_USER_WRITE_CONCERN_ERROR_INFO.writeConcernError
);
} catch (e) {
_err = e;
} finally {
cleanup(_err);
}
});
});
});
});
});
describe('retryable errors', () => {
describe('#needsRetryableWriteLabel', () => {
// Note the wireVersions used below are used to represent
// 8 - below server version 4.4
// 9 - above server version 4.4
const ABOVE_4_4 = 9;
const BELOW_4_4 = 8;
const tests: {
description: string;
result: boolean;
error: Error;
maxWireVersion: number;
}[] = [
{
description: 'a plain error',
result: false,
error: new Error('do not retry me!'),
maxWireVersion: BELOW_4_4
},
{
description: 'a MongoError with no code nor label',
result: false,
error: new MongoError('do not retry me!'),
maxWireVersion: BELOW_4_4
},
{
description: 'network error',
result: true,
error: new MongoNetworkError('socket bad, try again'),
maxWireVersion: BELOW_4_4
},
{
description: 'a MongoWriteConcernError with no code nor label',
result: false,
error: new MongoWriteConcernError({ message: 'empty wc error' }),
maxWireVersion: BELOW_4_4
},
{
description: 'a MongoWriteConcernError with a random label',
result: false,
error: new MongoWriteConcernError(
{ message: 'random label' },
{ errorLabels: ['myLabel'] }
),
maxWireVersion: BELOW_4_4
},
{
description: 'a MongoWriteConcernError with a retryable code above server 4.4',
result: false,
error: new MongoWriteConcernError({}, { code: 262 }),
maxWireVersion: ABOVE_4_4
},
{
description: 'a MongoWriteConcernError with a retryable code below server 4.4',
result: true,
error: new MongoWriteConcernError({}, { code: 262 }),
maxWireVersion: BELOW_4_4
},
{
description: 'a MongoWriteConcernError with a RetryableWriteError label below server 4.4',
result: false,
error: new MongoWriteConcernError({}, { errorLabels: ['RetryableWriteError'] }),
maxWireVersion: BELOW_4_4
},
{
description: 'a MongoWriteConcernError with a RetryableWriteError label above server 4.4',
result: false,
error: new MongoWriteConcernError({}, { errorLabels: ['RetryableWriteError'] }),
maxWireVersion: ABOVE_4_4
},
{
description: 'any MongoError with a RetryableWriteError label',
result: false,
error: (() => {
// These tests all use MongoWriteConcernError because
// its constructor is easier to call but any MongoError should work
const error = new MongoError('');
error.addErrorLabel('RetryableWriteError');
return error;
})(),
maxWireVersion: ABOVE_4_4
}
];
for (const { description, result, error, maxWireVersion } of tests) {
it(`${description} ${result ? 'needs' : 'does not need'} a retryable write label`, () => {
expect(needsRetryableWriteLabel(error, maxWireVersion)).to.be.equal(result);
});
}
});
describe('#isRetryableReadError', () => {
const tests: { description: string; result: boolean; error: MongoError }[] = [
{
description: 'plain error',
result: false,
// @ts-expect-error: passing in a plain error to test false case
error: new Error('do not retry me!')
},
{
description: 'An error code that is not retryable',
result: false,
error: new MongoServerError({ message: '', code: 1 })
},
{
description: 'An error code that is retryable',
result: true,
error: new MongoServerError({ message: '', code: 91 })
},
{
description: 'network error',
result: true,
error: new MongoNetworkError('socket bad, try again')
},
{
description: 'error with legacy not writable primary error message',
result: true,
error: new MongoError(LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.source)
},
{
description: 'error with node is recovering error message',
result: true,
error: new MongoError('node is recovering')
}
];
for (const { description, result, error } of tests) {
it(`${description} is${result ? '' : ' not'} a retryable read`, () => {
expect(isRetryableReadError(error)).to.be.equal(result);
});
}
});
});
describe('isResumableError()', () => {
describe('should return true', () => {
it('for MongoNetworkError regardless of wire version', () => {
expect(isResumableError(new MongoNetworkError('ah!'))).to.be.true;
expect(isResumableError(new MongoNetworkError('ah!'), 8)).to.be.true;
expect(isResumableError(new MongoNetworkError('ah!'), 9)).to.be.true;
expect(isResumableError(new MongoNetworkTimeoutError('ah!'))).to.be.true;
expect(isResumableError(new MongoNetworkTimeoutError('ah!'), 8)).to.be.true;
expect(isResumableError(new MongoNetworkTimeoutError('ah!'), 9)).to.be.true;
});
it('for labelless MongoError with CursorNotFound code regardless of wire version', () => {
const mongoError = new MongoError('ah!');
mongoError.code = MONGODB_ERROR_CODES.CursorNotFound;
expect(isResumableError(mongoError)).to.be.true;
expect(isResumableError(mongoError, 9)).to.be.true;
expect(isResumableError(mongoError, 8)).to.be.true;
});
it('for resumable codes if wireVersion is below 9 or unspecified', () => {
const mongoError = new MongoError('ah!');
mongoError.code = MONGODB_ERROR_CODES.ShutdownInProgress; // Shutdown in progress is resumable
expect(isResumableError(mongoError)).to.be.true;
expect(isResumableError(mongoError, 8)).to.be.true;
});
it('for labeled MongoError only if the wireVersion is at least 9', () => {
const mongoError = new MongoError('ah!');
mongoError.addErrorLabel(MongoErrorLabel.ResumableChangeStreamError);
expect(mongoError.hasErrorLabel(MongoErrorLabel.ResumableChangeStreamError)).to.be.true;
expect(isResumableError(mongoError, 9)).to.be.true;
});
});
describe('should return false', () => {
it('for errors that are not MongoError', () => {
expect(isResumableError(new Error('ah!'))).to.be.false;
expect(isResumableError(new TypeError('ah!'))).to.be.false;
});
it('for an error that is not a MongoError regardless of code property or wire version', () => {
// a plain error with and without wire version argument
expect(isResumableError(new Error('ah!'))).to.be.false;
expect(isResumableError(new Error('ah!'), 9)).to.be.false;
expect(isResumableError(new Error('ah!'), 8)).to.be.false;
const errorWithCode = new (class extends Error {
get code() {
throw new Error('code on a non-MongoError should not be inspected');
}
hasErrorLabel() {
throw new Error('hasErrorLabel should not be checked on a non-MongoError');
}
})();
// Expectations that prove this syntax provides what is desired for the test
expect(errorWithCode).to.be.instanceOf(Error);
expect(errorWithCode).to.not.be.instanceOf(MongoError);
// Testing that even coded and labeled non-MongoErrors will not get resumed
expect(isResumableError(errorWithCode)).to.be.false;
expect(isResumableError(errorWithCode, 8)).to.be.false;
expect(isResumableError(errorWithCode, 9)).to.be.false;
});
it('for nullish error argument regardless of wire version', () => {
expect(isResumableError()).to.be.false;
expect(isResumableError(undefined)).to.be.false;
expect(isResumableError(null)).to.be.false;
expect(isResumableError(null, null)).to.be.false;
expect(isResumableError(undefined, undefined)).to.be.false;
expect(isResumableError(null, undefined)).to.be.false;
expect(isResumableError(undefined, null)).to.be.false;
expect(isResumableError(null, 8)).to.be.false;
expect(isResumableError(null, 9)).to.be.false;
expect(isResumableError(undefined, 8)).to.be.false;
expect(isResumableError(undefined, 9)).to.be.false;
});
it('for resumable codes if wireVersion is at least 9', () => {
const mongoError = new MongoError('ah!');
mongoError.code = MONGODB_ERROR_CODES.ShutdownInProgress; // Shutdown in progress is resumable
expect(isResumableError(mongoError, 9)).to.be.false; // 4.4+ uses label only except for CursorNotFound
});
it('for non numeric code regardless of wire version', () => {
const mongoError = new MongoError('ah!');
mongoError.code = 'Random String';
expect(isResumableError(mongoError)).to.be.false;
expect(isResumableError(mongoError, 8)).to.be.false;
expect(isResumableError(mongoError, 9)).to.be.false;
});
it('for labeled error below wire version 9', () => {
const mongoError = new MongoError('ah!');
mongoError.addErrorLabel(MongoErrorLabel.ResumableChangeStreamError);
expect(mongoError.hasErrorLabel(MongoErrorLabel.ResumableChangeStreamError)).to.be.true;
expect(isResumableError(mongoError, 8)).to.be.false;
expect(isResumableError(mongoError)).to.be.false;
});
});
});
});