-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
error.test.js
238 lines (205 loc) · 7.65 KB
/
error.test.js
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
/* eslint no-empty: ["error", { "allowEmptyCatch": true }] */
'use strict';
const expect = require('chai').expect;
const mock = require('../tools/mock');
const { getSymbolFrom } = require('../tools/utils');
const { ReplSetFixture } = require('../tools/common');
const { ns } = require('../../src/utils');
const { Topology } = require('../../src/sdam/topology');
const { MongoNetworkError, MongoWriteConcernError } = require('../../src/index');
const { isRetryableEndTransactionError } = require('../../src/error');
const {
PoolClosedError: MongoPoolClosedError,
WaitQueueTimeoutError: MongoWaitQueueTimeoutError
} = require('../../src/cmap/errors');
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('#isRetryableEndTransactionError', function () {
context('when the error has a RetryableWriteError label', function () {
const error = new MongoNetworkError('');
error.addErrorLabel('RetryableWriteError');
it('returns true', function () {
expect(isRetryableEndTransactionError(error)).to.be.true;
});
});
context('when the error does not have a RetryableWriteError label', function () {
const error = new MongoNetworkError('');
error.addErrorLabel('InvalidLabel');
it('returns false', function () {
expect(isRetryableEndTransactionError(error)).to.be.false;
});
});
context('when the error does not have any label', function () {
const error = new MongoNetworkError('');
it('returns false', function () {
expect(isRetryableEndTransactionError(error)).to.be.false;
});
});
});
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('', { 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(() => mock.cleanup());
beforeEach(() => test.setup());
function makeAndConnectReplSet(cb) {
let invoked = false;
const replSet = new Topology(
[test.primaryServer.hostAddress(), test.firstSecondaryServer.hostAddress()],
{ replicaSet: 'rs' }
);
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 (doc.ismaster || doc.hello) {
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 (doc.ismaster || doc.hello) {
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);
}
});
});
});
});
});
});