Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 | 808x 808x 808x 808x 808x 808x 5183x 808x 17635769x 35506x 17635769x 7233x 2050x 5183x 1675x 3342x 3508x 20x 3678x 1150851x 406184x 808x 286440x 286440x 104978x 286440x 286440x 1704523x 1322485x 339381x 808x 1963896x 4x 808x 1931014x 2933x 808x 3560x 1557x 808x 200x 272x 808x 229x 4x 808x 4x 808x 3282x 467x 808x 4592x 4596x 808x 2075x 2079x 808x 4x 808x 9x 6x 808x 4x 808x 4x 808x 4x 808x 1309x 1309x 1309x 933x 808x 4x 808x 95x 99x 808x 6x 4x 808x 4x 808x 4x 808x 916x 920x 808x 480x 245x 808x 45593x 808x 199x 203x 808x 4x 808x 458x 4x 808x 1893656x 12x 808x 15295317x 15295317x 429591x 808x 22737x 25825x 808x 1335x 429x 808x 14313x 8314x 808x 1718x 1328x 808x 20x 4x 808x 474x 474x 4x 808x 2373x 2054x 319x 2373x 2373x 2373x 4x 808x 2361x 359x 808x 7860x 7860x 7860x 13845x 808x 808x 808x 301483x 6535x 294948x 294944x 222031x 72917x 416x 160x 256x 256x 72501x 72497x 4x 4x 4x 4x 4x 808x 145038x 808x 93686x 93686x 48836x 44850x 14932x 29918x 29918x 4x 29914x 29914x 4x 29910x 808x 808x 808x 350861x 256925x 93936x 267551x 220587x 46964x 46964x 808x 40299x 808x 303905x 8x 303897x 808x 808x 68884x 76x 68808x 566x 68242x 61155x 3748x 57407x 7087x 6842x 245x | import type { Document } from './bson'; import { type ClientBulkWriteError, type ClientBulkWriteResult } from './operations/client_bulk_write/common'; import type { ServerType } from './sdam/common'; import type { TopologyVersion } from './sdam/server_description'; import type { TopologyDescription } from './sdam/topology_description'; /** @public */ export type AnyError = MongoError | Error; /** * @internal * The legacy error message from the server that indicates the node is not a writable primary * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering */ export const LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE = new RegExp('not master', 'i'); /** * @internal * The legacy error message from the server that indicates the node is not a primary or secondary * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering */ export const LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE = new RegExp( 'not master or secondary', 'i' ); /** * @internal * The error message from the server that indicates the node is recovering * https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering */ export const NODE_IS_RECOVERING_ERROR_MESSAGE = new RegExp('node is recovering', 'i'); /** @internal MongoDB Error Codes */ export const MONGODB_ERROR_CODES = Object.freeze({ HostUnreachable: 6, HostNotFound: 7, AuthenticationFailed: 18, NetworkTimeout: 89, ShutdownInProgress: 91, PrimarySteppedDown: 189, ExceededTimeLimit: 262, SocketException: 9001, NotWritablePrimary: 10107, InterruptedAtShutdown: 11600, InterruptedDueToReplStateChange: 11602, NotPrimaryNoSecondaryOk: 13435, NotPrimaryOrSecondary: 13436, StaleShardVersion: 63, StaleEpoch: 150, StaleConfig: 13388, RetryChangeStream: 234, FailedToSatisfyReadPreference: 133, CursorNotFound: 43, LegacyNotPrimary: 10058, // WriteConcernTimeout is WriteConcernFailed on pre-8.1 servers WriteConcernTimeout: 64, NamespaceNotFound: 26, IllegalOperation: 20, MaxTimeMSExpired: 50, UnknownReplWriteConcern: 79, UnsatisfiableWriteConcern: 100, Reauthenticate: 391, ReadConcernMajorityNotAvailableYet: 134 } as const); // From spec https://github.com/mongodb/specifications/blob/921232976f9913cf17415b5ef937ee772e45e6ae/source/change-streams/change-streams.md#resumable-error export const GET_MORE_RESUMABLE_CODES = new Set<number>([ MONGODB_ERROR_CODES.HostUnreachable, MONGODB_ERROR_CODES.HostNotFound, MONGODB_ERROR_CODES.NetworkTimeout, MONGODB_ERROR_CODES.ShutdownInProgress, MONGODB_ERROR_CODES.PrimarySteppedDown, MONGODB_ERROR_CODES.ExceededTimeLimit, MONGODB_ERROR_CODES.SocketException, MONGODB_ERROR_CODES.NotWritablePrimary, MONGODB_ERROR_CODES.InterruptedAtShutdown, MONGODB_ERROR_CODES.InterruptedDueToReplStateChange, MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk, MONGODB_ERROR_CODES.NotPrimaryOrSecondary, MONGODB_ERROR_CODES.StaleShardVersion, MONGODB_ERROR_CODES.StaleEpoch, MONGODB_ERROR_CODES.StaleConfig, MONGODB_ERROR_CODES.RetryChangeStream, MONGODB_ERROR_CODES.FailedToSatisfyReadPreference, MONGODB_ERROR_CODES.CursorNotFound ]); /** @public */ export const MongoErrorLabel = Object.freeze({ RetryableWriteError: 'RetryableWriteError', TransientTransactionError: 'TransientTransactionError', UnknownTransactionCommitResult: 'UnknownTransactionCommitResult', ResumableChangeStreamError: 'ResumableChangeStreamError', HandshakeError: 'HandshakeError', ResetPool: 'ResetPool', PoolRequstedRetry: 'PoolRequstedRetry', InterruptInUseConnections: 'InterruptInUseConnections', NoWritesPerformed: 'NoWritesPerformed' } as const); /** @public */ export type MongoErrorLabel = (typeof MongoErrorLabel)[keyof typeof MongoErrorLabel]; /** @public */ export interface ErrorDescription extends Document { message?: string; errmsg?: string; $err?: string; errorLabels?: string[]; errInfo?: Document; } function isAggregateError(e: unknown): e is Error & { errors: Error[] } { return e != null && typeof e === 'object' && 'errors' in e && Array.isArray(e.errors); } /** * @public * @category Error * * @privateRemarks * mongodb-client-encryption has a dependency on this error, it uses the constructor with a string argument */ export class MongoError extends Error { /** @internal */ private readonly errorLabelSet: Set<string> = new Set(); public get errorLabels(): string[] { return Array.from(this.errorLabelSet); } /** * This is a number in MongoServerError and a string in MongoDriverError * @privateRemarks * Define the type override on the subclasses when we can use the override keyword */ code?: number | string; topologyVersion?: TopologyVersion; connectionGeneration?: number; override cause?: Error; /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } /** @internal */ static buildErrorMessage(e: unknown): string { if (typeof e === 'string') { return e; } if (isAggregateError(e) && e.message.length === 0) { return e.errors.length === 0 ? 'AggregateError has an empty errors array. Please check the `cause` property for more information.' : e.errors.map(({ message }) => message).join(', '); } return e != null && typeof e === 'object' && 'message' in e && typeof e.message === 'string' ? e.message : 'empty error message'; } override get name(): string { return 'MongoError'; } /** Legacy name for server error responses */ get errmsg(): string { return this.message; } /** * Checks the error to see if it has an error label * * @param label - The error label to check for * @returns returns true if the error has the provided error label */ hasErrorLabel(label: string): boolean { return this.errorLabelSet.has(label); } addErrorLabel(label: string): void { this.errorLabelSet.add(label); } } /** * An error coming from the mongo server * * @public * @category Error */ export class MongoServerError extends MongoError { /** Raw error result document returned by server. */ errorResponse: ErrorDescription; codeName?: string; writeConcernError?: Document; errInfo?: Document; ok?: number; [key: string]: any; /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: ErrorDescription) { super(message.message || message.errmsg || message.$err || 'n/a'); if (message.errorLabels) { for (const label of message.errorLabels) this.addErrorLabel(label); } this.errorResponse = message; for (const name in message) { if ( name !== 'errorLabels' && name !== 'errmsg' && name !== 'message' && name !== 'errorResponse' ) { this[name] = message[name]; } } } override get name(): string { return 'MongoServerError'; } } /** * An error generated by the driver * * @public * @category Error */ export class MongoDriverError extends MongoError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoDriverError'; } } /** * An error generated when the driver API is used incorrectly * * @privateRemarks * Should **never** be directly instantiated * * @public * @category Error */ export class MongoAPIError extends MongoDriverError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoAPIError'; } } /** * An error generated when the driver encounters unexpected input * or reaches an unexpected/invalid internal state. * * @privateRemarks * Should **never** be directly instantiated. * * @public * @category Error */ export class MongoRuntimeError extends MongoDriverError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoRuntimeError'; } } /** * An error generated when a primary server is marked stale, never directly thrown * * @public * @category Error */ export class MongoStalePrimaryError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoStalePrimaryError'; } } /** * An error generated when a batch command is re-executed after one of the commands in the batch * has failed * * @public * @category Error */ export class MongoBatchReExecutionError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message = 'This batch has already been executed, create new batch to execute') { super(message); } override get name(): string { return 'MongoBatchReExecutionError'; } } /** * An error generated when the driver fails to decompress * data received from the server. * * @public * @category Error */ export class MongoDecompressionError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoDecompressionError'; } } /** * An error thrown when the user attempts to operate on a database or collection through a MongoClient * that has not yet successfully called the "connect" method * * @public * @category Error */ export class MongoNotConnectedError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoNotConnectedError'; } } /** * An error generated when the user makes a mistake in the usage of transactions. * (e.g. attempting to commit a transaction with a readPreference other than primary) * * @public * @category Error */ export class MongoTransactionError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoTransactionError'; } } /** * An error generated when the user attempts to operate * on a session that has expired or has been closed. * * @public * @category Error */ export class MongoExpiredSessionError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message = 'Cannot use a session that has ended') { super(message); } override get name(): string { return 'MongoExpiredSessionError'; } } /** * A error generated when the user attempts to authenticate * via Kerberos, but fails to connect to the Kerberos client. * * @public * @category Error */ export class MongoKerberosError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoKerberosError'; } } /** * A error generated when the user attempts to authenticate * via AWS, but fails * * @public * @category Error */ export class MongoAWSError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoAWSError'; } } /** * A error generated when the user attempts to authenticate * via OIDC callbacks, but fails. * * @public * @category Error */ export class MongoOIDCError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoOIDCError'; } } /** * A error generated when the user attempts to authenticate * via Azure, but fails. * * @public * @category Error */ export class MongoAzureError extends MongoOIDCError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoAzureError'; } } /** * A error generated when the user attempts to authenticate * via GCP, but fails. * * @public * @category Error */ export class MongoGCPError extends MongoOIDCError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoGCPError'; } } /** * An error indicating that an error occurred when executing the bulk write. * * @public * @category Error */ export class MongoClientBulkWriteError extends MongoServerError { /** * Write concern errors that occurred while executing the bulk write. This list may have * multiple items if more than one server command was required to execute the bulk write. */ writeConcernErrors: Document[]; /** * Errors that occurred during the execution of individual write operations. This map will * contain at most one entry if the bulk write was ordered. */ writeErrors: Map<number, ClientBulkWriteError>; /** * The results of any successful operations that were performed before the error was * encountered. */ partialResult?: ClientBulkWriteResult; /** * Initialize the client bulk write error. * @param message - The error message. */ constructor(message: ErrorDescription) { super(message); this.writeConcernErrors = []; this.writeErrors = new Map(); } override get name(): string { return 'MongoClientBulkWriteError'; } } /** * An error indicating that an error occurred when processing bulk write results. * * @public * @category Error */ export class MongoClientBulkWriteCursorError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoClientBulkWriteCursorError'; } } /** * An error indicating that an error occurred on the client when executing a client bulk write. * * @public * @category Error */ export class MongoClientBulkWriteExecutionError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoClientBulkWriteExecutionError'; } } /** * An error generated when a ChangeStream operation fails to execute. * * @public * @category Error */ export class MongoChangeStreamError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoChangeStreamError'; } } /** * An error thrown when the user calls a function or method not supported on a tailable cursor * * @public * @category Error */ export class MongoTailableCursorError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message = 'Tailable cursor does not support this operation') { super(message); } override get name(): string { return 'MongoTailableCursorError'; } } /** An error generated when a GridFSStream operation fails to execute. * * @public * @category Error */ export class MongoGridFSStreamError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoGridFSStreamError'; } } /** * An error generated when a malformed or invalid chunk is * encountered when reading from a GridFSStream. * * @public * @category Error */ export class MongoGridFSChunkError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoGridFSChunkError'; } } /** * An error generated when a **parsable** unexpected response comes from the server. * This is generally an error where the driver in a state expecting a certain behavior to occur in * the next message from MongoDB but it receives something else. * This error **does not** represent an issue with wire message formatting. * * #### Example * When an operation fails, it is the driver's job to retry it. It must perform serverSelection * again to make sure that it attempts the operation against a server in a good state. If server * selection returns a server that does not support retryable operations, this error is used. * This scenario is unlikely as retryable support would also have been determined on the first attempt * but it is possible the state change could report a selectable server that does not support retries. * * @public * @category Error */ export class MongoUnexpectedServerResponseError extends MongoRuntimeError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoUnexpectedServerResponseError'; } } /** * @public * @category Error * * The `MongoOperationTimeoutError` class represents an error that occurs when an operation could not be completed within the specified `timeoutMS`. * It is generated by the driver in support of the "client side operation timeout" feature so inherits from `MongoDriverError`. * When `timeoutMS` is enabled `MongoServerError`s relating to `MaxTimeExpired` errors will be converted to `MongoOperationTimeoutError` * * @example * ```ts * try { * await blogs.insertOne(blogPost, { timeoutMS: 60_000 }) * } catch (error) { * if (error instanceof MongoOperationTimeoutError) { * console.log(`Oh no! writer's block!`, error); * } * } * ``` */ export class MongoOperationTimeoutError extends MongoDriverError { override get name(): string { return 'MongoOperationTimeoutError'; } } /** * An error thrown when the user attempts to add options to a cursor that has already been * initialized * * @public * @category Error */ export class MongoCursorInUseError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message = 'Cursor is already initialized') { super(message); } override get name(): string { return 'MongoCursorInUseError'; } } /** * An error generated when an attempt is made to operate * on a closed/closing server. * * @public * @category Error */ export class MongoServerClosedError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message = 'Server is closed') { super(message); } override get name(): string { return 'MongoServerClosedError'; } } /** * An error thrown when an attempt is made to read from a cursor that has been exhausted * * @public * @category Error */ export class MongoCursorExhaustedError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message?: string) { super(message || 'Cursor is exhausted'); } override get name(): string { return 'MongoCursorExhaustedError'; } } /** * An error generated when an attempt is made to operate on a * dropped, or otherwise unavailable, database. * * @public * @category Error */ export class MongoTopologyClosedError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message = 'Topology is closed') { super(message); } override get name(): string { return 'MongoTopologyClosedError'; } } /** @public */ export interface MongoNetworkErrorOptions { /** Indicates the timeout happened before a connection handshake completed */ beforeHandshake?: boolean; cause?: Error; } /** * An error indicating an issue with the network, including TCP errors and timeouts. * @public * @category Error */ export class MongoNetworkError extends MongoError { /** @internal */ public readonly beforeHandshake: boolean; /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: MongoNetworkErrorOptions) { super(message, { cause: options?.cause }); this.beforeHandshake = !!options?.beforeHandshake; } override get name(): string { return 'MongoNetworkError'; } } /** * An error indicating a network timeout occurred * @public * @category Error * * @privateRemarks * mongodb-client-encryption has a dependency on this error with an instanceof check */ export class MongoNetworkTimeoutError extends MongoNetworkError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: MongoNetworkErrorOptions) { super(message, options); } override get name(): string { return 'MongoNetworkTimeoutError'; } } /** * An error used when attempting to parse a value (like a connection string) * @public * @category Error */ export class MongoParseError extends MongoDriverError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoParseError'; } } /** * An error generated when the user supplies malformed or unexpected arguments * or when a required argument or field is not provided. * * * @public * @category Error */ export class MongoInvalidArgumentError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options?: { cause?: Error }) { super(message, options); } override get name(): string { return 'MongoInvalidArgumentError'; } } /** * An error generated when a feature that is not enabled or allowed for the current server * configuration is used * * * @public * @category Error */ export class MongoCompatibilityError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoCompatibilityError'; } } /** * An error generated when the user fails to provide authentication credentials before attempting * to connect to a mongo server instance. * * * @public * @category Error */ export class MongoMissingCredentialsError extends MongoAPIError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string) { super(message); } override get name(): string { return 'MongoMissingCredentialsError'; } } /** * An error generated when a required module or dependency is not present in the local environment * * @public * @category Error */ export class MongoMissingDependencyError extends MongoAPIError { dependencyName: string; /** @remarks This property is assigned in the `Error` constructor. */ declare cause: Error; /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, options: { cause: Error; dependencyName: string }) { super(message, options); this.dependencyName = options.dependencyName; } override get name(): string { return 'MongoMissingDependencyError'; } } /** * An error signifying a general system issue * @public * @category Error */ export class MongoSystemError extends MongoError { /** An optional reason context, such as an error saved during flow of monitoring and selecting servers */ reason?: TopologyDescription; /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, reason: TopologyDescription) { if (reason && reason.error) { super(MongoError.buildErrorMessage(reason.error.message || reason.error), { cause: reason.error }); } else { super(message); } Eif (reason) { this.reason = reason; } this.code = reason.error?.code; } override get name(): string { return 'MongoSystemError'; } } /** * An error signifying a client-side server selection error * @public * @category Error */ export class MongoServerSelectionError extends MongoSystemError { /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(message: string, reason: TopologyDescription) { super(message, reason); } override get name(): string { return 'MongoServerSelectionError'; } } /** * The type of the result property of MongoWriteConcernError * @public */ export interface WriteConcernErrorResult { writeConcernError: { code: number; errmsg: string; codeName?: string; errInfo?: Document; }; ok: number; code?: number; errorLabels?: string[]; [x: string | number]: unknown; } /** * An error thrown when the server reports a writeConcernError * @public * @category Error */ export class MongoWriteConcernError extends MongoServerError { /** The result document */ result: Document; /** * **Do not use this constructor!** * * Meant for internal use only. * * @remarks * This class is only meant to be constructed within the driver. This constructor is * not subject to semantic versioning compatibility guarantees and may change at any time. * * @public **/ constructor(result: WriteConcernErrorResult) { super({ ...result.writeConcernError, ...result }); this.errInfo = result.writeConcernError.errInfo; this.result = result; } override get name(): string { return 'MongoWriteConcernError'; } } // https://github.com/mongodb/specifications/blob/master/source/retryable-reads/retryable-reads.md#retryable-error const RETRYABLE_READ_ERROR_CODES = new Set<number>([ MONGODB_ERROR_CODES.HostUnreachable, MONGODB_ERROR_CODES.HostNotFound, MONGODB_ERROR_CODES.NetworkTimeout, MONGODB_ERROR_CODES.ShutdownInProgress, MONGODB_ERROR_CODES.PrimarySteppedDown, MONGODB_ERROR_CODES.SocketException, MONGODB_ERROR_CODES.NotWritablePrimary, MONGODB_ERROR_CODES.InterruptedAtShutdown, MONGODB_ERROR_CODES.InterruptedDueToReplStateChange, MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk, MONGODB_ERROR_CODES.NotPrimaryOrSecondary, MONGODB_ERROR_CODES.ExceededTimeLimit, MONGODB_ERROR_CODES.ReadConcernMajorityNotAvailableYet ]); // see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.md#terms const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES; export function needsRetryableWriteLabel( error: Error, maxWireVersion: number, serverType: ServerType ): boolean { // pre-4.4 server, then the driver adds an error label for every valid case // execute operation will only inspect the label, code/message logic is handled here if (error instanceof MongoNetworkError) { return true; } if (error instanceof MongoError) { if ( (maxWireVersion >= 9 || isRetryableWriteError(error)) && !error.hasErrorLabel(MongoErrorLabel.HandshakeError) ) { // If we already have the error label no need to add it again. 4.4+ servers add the label. // In the case where we have a handshake error, need to fall down to the logic checking // the codes. return false; } } if (error instanceof MongoWriteConcernError) { if (serverType === 'Mongos' && maxWireVersion < 9) { // use original top-level code from server response return RETRYABLE_WRITE_ERROR_CODES.has(error.result.code ?? 0); } const code = error.result.writeConcernError.code ?? Number(error.code); return RETRYABLE_WRITE_ERROR_CODES.has(Number.isNaN(code) ? 0 : code); } if (error instanceof MongoError) { return RETRYABLE_WRITE_ERROR_CODES.has(Number(error.code)); } const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message); Iif (isNotWritablePrimaryError) { return true; } const isNodeIsRecoveringError = NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message); Iif (isNodeIsRecoveringError) { return true; } return false; } export function isRetryableWriteError(error: MongoError): boolean { return ( error.hasErrorLabel(MongoErrorLabel.RetryableWriteError) || error.hasErrorLabel(MongoErrorLabel.PoolRequstedRetry) ); } /** Determines whether an error is something the driver should attempt to retry */ export function isRetryableReadError(error: MongoError): boolean { const hasRetryableErrorCode = typeof error.code === 'number' ? RETRYABLE_READ_ERROR_CODES.has(error.code) : false; if (hasRetryableErrorCode) { return true; } if (error instanceof MongoNetworkError) { return true; } const isNotWritablePrimaryError = LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(error.message); if (isNotWritablePrimaryError) { return true; } const isNodeIsRecoveringError = NODE_IS_RECOVERING_ERROR_MESSAGE.test(error.message); if (isNodeIsRecoveringError) { return true; } return false; } const SDAM_RECOVERING_CODES = new Set<number>([ MONGODB_ERROR_CODES.ShutdownInProgress, MONGODB_ERROR_CODES.PrimarySteppedDown, MONGODB_ERROR_CODES.InterruptedAtShutdown, MONGODB_ERROR_CODES.InterruptedDueToReplStateChange, MONGODB_ERROR_CODES.NotPrimaryOrSecondary ]); const SDAM_NOT_PRIMARY_CODES = new Set<number>([ MONGODB_ERROR_CODES.NotWritablePrimary, MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk, MONGODB_ERROR_CODES.LegacyNotPrimary ]); const SDAM_NODE_SHUTTING_DOWN_ERROR_CODES = new Set<number>([ MONGODB_ERROR_CODES.InterruptedAtShutdown, MONGODB_ERROR_CODES.ShutdownInProgress ]); function isRecoveringError(err: MongoError) { if (typeof err.code === 'number') { // If any error code exists, we ignore the error.message return SDAM_RECOVERING_CODES.has(err.code); } return ( LEGACY_NOT_PRIMARY_OR_SECONDARY_ERROR_MESSAGE.test(err.message) || NODE_IS_RECOVERING_ERROR_MESSAGE.test(err.message) ); } function isNotWritablePrimaryError(err: MongoError) { if (typeof err.code === 'number') { // If any error code exists, we ignore the error.message return SDAM_NOT_PRIMARY_CODES.has(err.code); } Iif (isRecoveringError(err)) { return false; } return LEGACY_NOT_WRITABLE_PRIMARY_ERROR_MESSAGE.test(err.message); } export function isNodeShuttingDownError(err: MongoError): boolean { return !!(typeof err.code === 'number' && SDAM_NODE_SHUTTING_DOWN_ERROR_CODES.has(err.code)); } /** * Determines whether SDAM can recover from a given error. If it cannot * then the pool will be cleared, and server state will completely reset * locally. * * @see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md#not-writable-primary-and-node-is-recovering */ export function isSDAMUnrecoverableError(error: MongoError): boolean { // NOTE: null check is here for a strictly pre-CMAP world, a timeout or // close event are considered unrecoverable if (error instanceof MongoParseError || error == null) { return true; } return isRecoveringError(error) || isNotWritablePrimaryError(error); } export function isNetworkTimeoutError(err: MongoError): err is MongoNetworkError { return !!(err instanceof MongoNetworkError && err.message.match(/timed out/)); } export function isResumableError(error?: Error, wireVersion?: number): boolean { if (error == null || !(error instanceof MongoError)) { return false; } if (error instanceof MongoNetworkError) { return true; } if (wireVersion != null && wireVersion >= 9) { // DRIVERS-1308: For 4.4 drivers running against 4.4 servers, drivers will add a special case to treat the CursorNotFound error code as resumable if (error.code === MONGODB_ERROR_CODES.CursorNotFound) { return true; } return error.hasErrorLabel(MongoErrorLabel.ResumableChangeStreamError); } if (typeof error.code === 'number') { return GET_MORE_RESUMABLE_CODES.has(error.code); } return false; } |