|
1 |
| -import { isMongoNetworkTimeoutError, mongo } from '@powersync/lib-service-mongodb'; |
| 1 | +import { isMongoNetworkTimeoutError, isMongoServerError, mongo } from '@powersync/lib-service-mongodb'; |
2 | 2 | import {
|
3 | 3 | container,
|
4 | 4 | DatabaseConnectionError,
|
@@ -605,13 +605,7 @@ export class ChangeStream {
|
605 | 605 | }
|
606 | 606 |
|
607 | 607 | const originalChangeDocument = await stream.tryNext().catch((e) => {
|
608 |
| - if (isMongoNetworkTimeoutError(e)) { |
609 |
| - // This typically has an unhelpful message like "connection 2 to 159.41.94.47:27017 timed out". |
610 |
| - // We wrap the error to make it more useful. |
611 |
| - throw new DatabaseConnectionError(ErrorCode.PSYNC_S1345, `Timeout while reading MongoDB ChangeStream`, e); |
612 |
| - } else { |
613 |
| - throw new DatabaseConnectionError(ErrorCode.PSYNC_S1346, `Error reading MongoDB ChangeStream`, e); |
614 |
| - } |
| 608 | + throw mapChangeStreamError(e); |
615 | 609 | });
|
616 | 610 | // The stream was closed, we will only ever receive `null` from it
|
617 | 611 | if (!originalChangeDocument && stream.closed) {
|
@@ -793,3 +787,21 @@ async function touch() {
|
793 | 787 | // or reduce PING_INTERVAL here.
|
794 | 788 | return container.probes.touch();
|
795 | 789 | }
|
| 790 | + |
| 791 | +function mapChangeStreamError(e: any) { |
| 792 | + if (isMongoNetworkTimeoutError(e)) { |
| 793 | + // This typically has an unhelpful message like "connection 2 to 159.41.94.47:27017 timed out". |
| 794 | + // We wrap the error to make it more useful. |
| 795 | + throw new DatabaseConnectionError(ErrorCode.PSYNC_S1345, `Timeout while reading MongoDB ChangeStream`, e); |
| 796 | + } else if ( |
| 797 | + isMongoServerError(e) && |
| 798 | + e.codeName == 'NoMatchingDocument' && |
| 799 | + e.errmsg?.includes('post-image was not found') |
| 800 | + ) { |
| 801 | + throw new ChangeStreamInvalidatedError(e.errmsg, e); |
| 802 | + } else if (isMongoServerError(e) && e.hasErrorLabel('NonResumableChangeStreamError')) { |
| 803 | + throw new ChangeStreamInvalidatedError(e.message, e); |
| 804 | + } else { |
| 805 | + throw new DatabaseConnectionError(ErrorCode.PSYNC_S1346, `Error reading MongoDB ChangeStream`, e); |
| 806 | + } |
| 807 | +} |
0 commit comments