Skip to content

Commit

Permalink
fix(wallet): throw reason of internal error to handle it in wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed May 25, 2023
1 parent 332d1b5 commit 276699b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/aepp-wallet-communication/rpc/RpcClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RpcError, RpcInternalError, RpcMethodNotFoundError } from '../schema';
import BrowserConnection from '../connection/Browser';
import { InvalidRpcMessageError, MissingCallbackError } from '../../utils/errors';
import { ensureError } from '../../utils/other';

interface JsonRpcRequest {
jsonrpc: '2.0';
Expand Down Expand Up @@ -64,17 +65,24 @@ export default class RpcClient <

const request = msg as JsonRpcRequest;
let result;
let error;
let error: Error | undefined;
try {
if (!(request.method in this.#methods)) throw new RpcMethodNotFoundError();
const methodName = request.method as keyof LocalApi;
result = await this.#methods[methodName](request.params, origin);
} catch (e) {
error = e instanceof RpcError ? e : new RpcInternalError();
ensureError(e);
error = e;
}
if (request.id != null) {
this.#sendResponse(request.id, request.method as keyof LocalApi, result, error);
this.#sendResponse(
request.id,
request.method as keyof LocalApi,
result,
error == null || error instanceof RpcError ? error : new RpcInternalError(),
);
}
if (error != null && !(error instanceof RpcError)) throw error;
}

#sendRequest(
Expand Down

0 comments on commit 276699b

Please sign in to comment.