Skip to content

Commit 632a682

Browse files
committed
update tests and impl
1 parent abfd358 commit 632a682

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

src/error.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { Document } from './bson';
1+
import type { Document, ObjectId } from './bson';
22
import {
33
type ClientBulkWriteError,
44
type ClientBulkWriteResult
55
} from './operations/client_bulk_write/common';
66
import type { ServerType } from './sdam/common';
7-
import type { TopologyVersion } from './sdam/server_description';
7+
import type { ServerDescription, TopologyVersion } from './sdam/server_description';
88
import type { TopologyDescription } from './sdam/topology_description';
99

1010
/** @public */
@@ -340,6 +340,43 @@ export class MongoRuntimeError extends MongoDriverError {
340340
}
341341
}
342342

343+
/**
344+
* An error generated when a primary server is marked stale, never directly thrown
345+
* @privateRemarks
346+
* Should **never** be directly instantiated.
347+
*
348+
* @public
349+
* @category Error
350+
*/
351+
export class MongoStalePrimaryError extends MongoRuntimeError {
352+
/**
353+
* **Do not use this constructor!**
354+
*
355+
* Meant for internal use only.
356+
*
357+
* @remarks
358+
* This class is only meant to be constructed within the driver. This constructor is
359+
* not subject to semantic versioning compatibility guarantees and may change at any time.
360+
*
361+
* @public
362+
**/
363+
constructor(
364+
serverDescription: ServerDescription,
365+
maxSetVersion: number | null,
366+
maxElectionId: ObjectId | null,
367+
options?: { cause?: Error }
368+
) {
369+
super(
370+
`primary marked stale due to electionId/setVersion mismatch: server setVersion: ${serverDescription.setVersion}, server electionId: ${serverDescription.electionId}, topology setVersion: ${maxSetVersion}, topology electionId: ${maxElectionId}`,
371+
options
372+
);
373+
}
374+
375+
override get name(): string {
376+
return 'MongoStalePrimaryError';
377+
}
378+
}
379+
343380
/**
344381
* An error generated when a batch command is re-executed after one of the commands in the batch
345382
* has failed

src/sdam/topology_description.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EJSON, type ObjectId } from '../bson';
22
import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants';
3-
import { MongoError, MongoRuntimeError } from '../error';
3+
import { type MongoError, MongoRuntimeError, MongoStalePrimaryError } from '../error';
44
import { compareObjectId, shuffle } from '../utils';
55
import { ServerType, TopologyType } from './common';
66
import { ServerDescription } from './server_description';
@@ -401,9 +401,7 @@ function updateRsFromPrimary(
401401
serverDescriptions.set(
402402
serverDescription.address,
403403
new ServerDescription(serverDescription.address, undefined, {
404-
error: new MongoError(
405-
`primary marked stale due to electionId/setVersion mismatch: server setVersion: ${serverDescription.setVersion}, server electionId: ${serverDescription.electionId}, topology setVersion: ${maxSetVersion}, topology electionId: ${maxElectionId}`
406-
)
404+
error: new MongoStalePrimaryError(serverDescription, maxSetVersion, maxElectionId)
407405
})
408406
);
409407

@@ -421,9 +419,7 @@ function updateRsFromPrimary(
421419
serverDescriptions.set(
422420
serverDescription.address,
423421
new ServerDescription(serverDescription.address, undefined, {
424-
error: new MongoError(
425-
`primary marked stale due to electionId/setVersion mismatch: server setVersion: ${serverDescription.setVersion}, server electionId: ${serverDescription.electionId}, topology setVersion: ${maxSetVersion}, topology electionId: ${maxElectionId}`
426-
)
422+
error: new MongoStalePrimaryError(serverDescription, maxSetVersion, maxElectionId)
427423
})
428424
);
429425

@@ -449,9 +445,7 @@ function updateRsFromPrimary(
449445
serverDescriptions.set(
450446
address,
451447
new ServerDescription(server.address, undefined, {
452-
error: new MongoError(
453-
`primary marked stale due to electionId/setVersion mismatch: server setVersion: ${server.setVersion}, server electionId: ${server.electionId}, topology setVersion: ${maxSetVersion}, topology electionId: ${maxElectionId}`
454-
)
448+
error: new MongoStalePrimaryError(serverDescription, maxSetVersion, maxElectionId)
455449
})
456450
);
457451

test/unit/assorted/server_discovery_and_monitoring.spec.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function normalizeServerDescription(serverDescription) {
271271
// it as `Unknown`.
272272
serverDescription.type = 'Unknown';
273273
}
274-
serverDescription.error == serverDescription?.error?.message;
274+
//serverDescription.error = serverDescription?.error?.message;
275275

276276
return serverDescription;
277277
}
@@ -475,7 +475,7 @@ function assertTopologyDescriptionOutcomeExpectations(
475475
} else if (expectedKey !== 'error') {
476476
expect(actualServer).to.have.deep.property(expectedKey, expectedValue);
477477
} else {
478-
// Check that if we have
478+
// pull the message off the error if it is present
479479
expect(actualServer).to.have.deep.property(expectedKey).instanceof(MongoError);
480480
expect(actualServer).property(expectedKey).property('message').includes(expectedValue);
481481
}

0 commit comments

Comments
 (0)