Skip to content

Commit

Permalink
add socket error handling (#33)
Browse files Browse the repository at this point in the history
* add socket error handling

* add support for customer (demo) BE errors in socket

* Add socket error handling
  • Loading branch information
goweiss authored Jun 27, 2024
1 parent 82574c7 commit c6affed
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/services/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ export interface IAssetBalance {
blockHash?: string;
}

type RpcResponse =
| {
response: unknown;
}
| {
error: {
message: string;
code?: number;
};
};

export type TMessageHandler = (message: any) => Promise<void>;
export type TTxHandler = (tx: ITransactionData) => void;

Expand Down Expand Up @@ -237,10 +248,16 @@ export class ApiService {

public async sendMessage(deviceId: string, message: string): Promise<any> {
if (this.socket.connected) {
return await this.socket.emitWithAck("rpc", deviceId, message);
} else {
return this._postCall(`api/devices/${deviceId}/rpc`, { message });
const response: RpcResponse = await this.socket.emitWithAck("rpc", deviceId, message);
if (!("response" in response)) {
console.error("Failed to invoke RPC", response?.error);
throw new Error("Failed to invoke RPC");
}

return response.response;
}

return this._postCall(`api/devices/${deviceId}/rpc`, { message });
}

public async getWeb3Connections(deviceId: string): Promise<IWeb3Session[]> {
Expand Down

0 comments on commit c6affed

Please sign in to comment.