(message: ServerErrorMessage)
| 1150 | } |
| 1151 | |
| 1152 | private async handleErrorMessage(message: ServerErrorMessage) { |
| 1153 | const { error, metadata, messageType } = message.payload; |
| 1154 | this.logger.error(error.name, metadata); |
| 1155 | switch (error.name) { |
| 1156 | case 'MalformedMessagePayloadError': |
| 1157 | case 'UnrecognizedMessageTypeError': |
| 1158 | this.logger.warn( |
| 1159 | 'You sent a malformed message to the server. This might occur if your client is not up to date with the server. Please ensure your client is updated.' |
| 1160 | ); |
| 1161 | break; |
| 1162 | // On a remote read error, default to disconnecting the query |
| 1163 | // You will still send triples, but you wont receive updates |
| 1164 | case 'QuerySyncError': |
| 1165 | const queryKey = metadata?.queryKey; |
| 1166 | if (queryKey) { |
| 1167 | const query = this.queries.get(queryKey); |
| 1168 | if (query) { |
| 1169 | const parsedError = TriplitError.fromJson(error); |
| 1170 | query.syncState = 'ERROR'; |
| 1171 | for (const callback of query.syncStateCallbacks) { |
| 1172 | // TODO: include metadata (inner error) |
| 1173 | await callback('ERROR', parsedError); |
| 1174 | } |
| 1175 | } |
| 1176 | this.disconnectQuery(queryKey); |
| 1177 | } |
| 1178 | } |
| 1179 | if (messageType === 'CHANGES') { |
| 1180 | if (this.client.awaitReady) await this.client.awaitReady; |
| 1181 | const kvTx = this.client.db.kv.transact(); |
| 1182 | const outbox = this.client.db.entityStore.doubleBuffer; |
| 1183 | |
| 1184 | // can we have the server send this back instead of reading the potentially |
| 1185 | // unstable buffer? |
| 1186 | const failedChanges = await outbox.getLockedBuffer().getChanges(kvTx); |
| 1187 | |
| 1188 | // rebase the unlocked buffer on the failed locked buffer |
| 1189 | await outbox |
| 1190 | .getLockedBuffer() |
| 1191 | .write(kvTx, await outbox.getUnlockedBuffer().getChanges(kvTx)); |
| 1192 | await outbox.getUnlockedBuffer().clear(kvTx); |
| 1193 | await kvTx.commit(); |
| 1194 | |
| 1195 | // now we can switch the buffers so that the |
| 1196 | // client can write to the unlocked buffer |
| 1197 | outbox.lockAndSwitchBuffers(); |
| 1198 | this.syncInProgress = false; |
| 1199 | for (const handler of this.onFailureToSyncWritesSubscribers) { |
| 1200 | await handler(error, failedChanges); |
| 1201 | } |
| 1202 | for (const collection in failedChanges) { |
| 1203 | // TODO: layer in deletes |
| 1204 | for (const [id, change] of failedChanges[collection].sets) { |
| 1205 | const errorCallback = this.entitySyncErrorSubscribers.get( |
| 1206 | collection, |
| 1207 | id |
| 1208 | ); |
| 1209 | if (errorCallback) { |
no test coverage detected