From 6eaba28b5f2bbdad637446f1b55564959f6c1b6f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 1 Jul 2024 14:09:08 +0200 Subject: [PATCH] Handle error messages properly --- src/spacetimedb.ts | 86 +++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/src/spacetimedb.ts b/src/spacetimedb.ts index c448b3c..a16e57e 100644 --- a/src/spacetimedb.ts +++ b/src/spacetimedb.ts @@ -251,49 +251,55 @@ export class SpacetimeDBClient { } } else if (message instanceof TransactionUpdateMessage) { const reducerName = message.event.reducerName; - const reducer: any | undefined = reducerName - ? SpacetimeDBClient.getReducerClass(reducerName) - : undefined; - - let reducerEvent: ReducerEvent | undefined; - let reducerArgs: any; - if (reducer && message.event.status === "committed") { - let adapter: ReducerArgsAdapter = new BinaryReducerArgsAdapter( - new BinaryAdapter( - new BinaryReader(message.event.args as Uint8Array) - ) - ); - - reducerArgs = reducer.deserializeArgs(adapter); - } - - reducerEvent = new ReducerEvent( - message.event.identity, - message.event.address, - message.event.originalReducerName, - message.event.status, - message.event.message, - reducerArgs - ); - for (let tableUpdate of message.tableUpdates) { - const tableName = tableUpdate.tableName; - const entityClass = SpacetimeDBClient.getTableClass(tableName); - const table = this.db.getOrCreateTable( - tableUpdate.tableName, - undefined, - entityClass + if (reducerName == "") { + let errorMessage = message.event.message; + console.error(`Received an error from the database: ${errorMessage}`); + } else { + const reducer: any | undefined = reducerName + ? SpacetimeDBClient.getReducerClass(reducerName) + : undefined; + + let reducerEvent: ReducerEvent | undefined; + let reducerArgs: any; + if (reducer && message.event.status === "committed") { + let adapter: ReducerArgsAdapter = new BinaryReducerArgsAdapter( + new BinaryAdapter( + new BinaryReader(message.event.args as Uint8Array) + ) + ); + + reducerArgs = reducer.deserializeArgs(adapter); + } + + reducerEvent = new ReducerEvent( + message.event.identity, + message.event.address, + message.event.originalReducerName, + message.event.status, + message.event.message, + reducerArgs ); - table.applyOperations(tableUpdate.operations, reducerEvent); - } - - if (reducer) { - this.emitter.emit( - "reducer:" + reducerName, - reducerEvent, - ...(reducerArgs || []) - ); + for (let tableUpdate of message.tableUpdates) { + const tableName = tableUpdate.tableName; + const entityClass = SpacetimeDBClient.getTableClass(tableName); + const table = this.db.getOrCreateTable( + tableUpdate.tableName, + undefined, + entityClass + ); + + table.applyOperations(tableUpdate.operations, reducerEvent); + } + + if (reducer) { + this.emitter.emit( + "reducer:" + reducerName, + reducerEvent, + ...(reducerArgs || []) + ); + } } } else if (message instanceof IdentityTokenMessage) { this.identity = message.identity;