MCPcopy
hub / github.com/mongodb/node-mongodb-native / handleError

Method handleError

src/sdam/server.ts:390–444  ·  view source on GitHub ↗

* Handle SDAM error * @internal

(error: AnyError, connection?: Connection)

Source from the content-addressed store, hash-verified

388 * @internal
389 */
390 handleError(error: AnyError, connection?: Connection) {
391 if (!(error instanceof MongoError)) {
392 return;
393 }
394
395 if (isStaleError(this, error)) {
396 return;
397 }
398
399 const isNetworkNonTimeoutError =
400 error instanceof MongoNetworkError && !(error instanceof MongoNetworkTimeoutError);
401 const isNetworkTimeoutBeforeHandshakeError =
402 error instanceof MongoNetworkError && error.beforeHandshake;
403 const isAuthOrEstablishmentHandshakeError = error.hasErrorLabel(MongoErrorLabel.HandshakeError);
404 const isSystemOverloadError = error.hasErrorLabel(MongoErrorLabel.SystemOverloadedError);
405
406 // Perhaps questionable and divergent from the spec, but considering MongoParseErrors like state change errors was legacy behavior.
407 if (isStateChangeError(error) || error instanceof MongoParseError) {
408 const shouldClearPool = isNodeShuttingDownError(error);
409
410 // from the SDAM spec: The driver MUST synchronize clearing the pool with updating the topology.
411 // In load balanced mode: there is no monitoring, so there is no topology to update. We simply clear the pool.
412 // For other topologies: the `ResetPool` label instructs the topology to clear the server's pool in `updateServer()`.
413 if (!this.loadBalanced) {
414 if (shouldClearPool) {
415 error.addErrorLabel(MongoErrorLabel.ResetPool);
416 }
417 markServerUnknown(this, error);
418 queueMicrotask(() => this.requestCheck());
419 return;
420 }
421
422 if (connection && shouldClearPool) {
423 this.pool.clear({ serviceId: connection.serviceId });
424 }
425 } else if (
426 isNetworkNonTimeoutError ||
427 isNetworkTimeoutBeforeHandshakeError ||
428 isAuthOrEstablishmentHandshakeError
429 ) {
430 // Do NOT clear the pool if we encounter a system overloaded error.
431 if (isSystemOverloadError) {
432 return;
433 }
434 // from the SDAM spec: The driver MUST synchronize clearing the pool with updating the topology.
435 // In load balanced mode: there is no monitoring, so there is no topology to update. We simply clear the pool.
436 // For other topologies: the `ResetPool` label instructs the topology to clear the server's pool in `updateServer()`.
437 if (!this.loadBalanced) {
438 error.addErrorLabel(MongoErrorLabel.ResetPool);
439 markServerUnknown(this, error);
440 } else if (connection) {
441 this.pool.clear({ serviceId: connection.serviceId });
442 }
443 }
444 }
445
446 /**
447 * Ensure that error is properly decorated and internal state is updated before throwing

Callers 4

commandMethod · 0.95
decorateCommandErrorMethod · 0.95
server.test.tsFile · 0.45
createConnectionMethod · 0.45

Calls 8

requestCheckMethod · 0.95
isStateChangeErrorFunction · 0.90
isNodeShuttingDownErrorFunction · 0.90
isStaleErrorFunction · 0.85
markServerUnknownFunction · 0.85
addErrorLabelMethod · 0.80
hasErrorLabelMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected