* Ensure that error is properly decorated and internal state is updated before throwing * @internal
(
connection: Connection,
cmd: Document,
options: CommandOptions | GetMoreOptions | undefined,
error: unknown
)
| 448 | * @internal |
| 449 | */ |
| 450 | private decorateCommandError( |
| 451 | connection: Connection, |
| 452 | cmd: Document, |
| 453 | options: CommandOptions | GetMoreOptions | undefined, |
| 454 | error: unknown |
| 455 | ): Error { |
| 456 | if (typeof error !== 'object' || error == null || !('name' in error)) { |
| 457 | throw new MongoRuntimeError('An unexpected error type: ' + typeof error); |
| 458 | } |
| 459 | |
| 460 | if (error.name === 'AbortError' && 'cause' in error && error.cause instanceof MongoError) { |
| 461 | error = error.cause; |
| 462 | } |
| 463 | |
| 464 | if (!(error instanceof MongoError)) { |
| 465 | // Node.js or some other error we have not special handling for |
| 466 | return error as Error; |
| 467 | } |
| 468 | |
| 469 | if (connectionIsStale(this.pool, connection)) { |
| 470 | return error; |
| 471 | } |
| 472 | |
| 473 | const session = options?.session; |
| 474 | if (error instanceof MongoNetworkError) { |
| 475 | if (session && !session.hasEnded && session.serverSession) { |
| 476 | session.serverSession.isDirty = true; |
| 477 | } |
| 478 | |
| 479 | // inActiveTransaction check handles commit and abort. |
| 480 | if ( |
| 481 | inActiveTransaction(session, cmd) && |
| 482 | !error.hasErrorLabel(MongoErrorLabel.TransientTransactionError) |
| 483 | ) { |
| 484 | error.addErrorLabel(MongoErrorLabel.TransientTransactionError); |
| 485 | } |
| 486 | |
| 487 | if ( |
| 488 | (isRetryableWritesEnabled(this.topology) || isTransactionCommand(cmd)) && |
| 489 | supportsRetryableWrites(this) && |
| 490 | !inActiveTransaction(session, cmd) |
| 491 | ) { |
| 492 | error.addErrorLabel(MongoErrorLabel.RetryableWriteError); |
| 493 | } |
| 494 | } else { |
| 495 | if ( |
| 496 | (isRetryableWritesEnabled(this.topology) || isTransactionCommand(cmd)) && |
| 497 | needsRetryableWriteLabel(error, maxWireVersion(this), this.description.type) && |
| 498 | !inActiveTransaction(session, cmd) |
| 499 | ) { |
| 500 | error.addErrorLabel(MongoErrorLabel.RetryableWriteError); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if ( |
| 505 | session && |
| 506 | session.isPinned && |
| 507 | error.hasErrorLabel(MongoErrorLabel.TransientTransactionError) |
no test coverage detected