* Handle SDAM error * @internal
(error: AnyError, connection?: Connection)
| 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 |
no test coverage detected