Skip to content

Commit

Permalink
Feature: Catch exceptions in communicator (#182)
Browse files Browse the repository at this point in the history
* figuring out tests

* fix test

* remove console.log
  • Loading branch information
mmv08 authored Jun 16, 2021
1 parent 826dd31 commit 97920f8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
24 changes: 24 additions & 0 deletions packages/safe-apps-sdk/src/communication/communicator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SDKMessageEvent } from './../types/messaging';
import { Methods } from './methods';
import PostMessageCommunicator from './';
import { MessageFormatter } from './messageFormatter';

describe('PostMessageCommunicator', () => {
test('Throws in case of an error response', async () => {
const error = 'Problem processing the request';
const messageHandler = (event: SDKMessageEvent) => {
const requestId = event.data.id;
const response = MessageFormatter.makeErrorResponse(requestId, error, '1.0.0');

window.parent.postMessage(response, '*');
};
window.parent.addEventListener('message', messageHandler);

const communicator = new PostMessageCommunicator();
// @ts-expect-error mock isValidMessage because source check couldn't be passed otherwise, filter out requests
communicator.isValidMessage = (msg) => typeof msg.data.success !== 'undefined';

await expect(communicator.send(Methods.getSafeInfo, undefined)).rejects.toThrow(error);
window.removeEventListener('message', messageHandler);
});
});
11 changes: 8 additions & 3 deletions packages/safe-apps-sdk/src/communication/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InterfaceMessageEvent, Communicator, Response } from '../types';
import { MessageFormatter } from './messageFormatter';
import { Methods } from './methods';
import { InterfaceMessageEvent, Communicator, Response, SuccessResponse } from '../types';

// eslint-disable-next-line
type Callback = (response: any) => void;
Expand Down Expand Up @@ -52,16 +52,21 @@ class PostMessageCommunicator implements Communicator {
}
};

public send = <M extends Methods, P, R>(method: M, params: P): Promise<Response<R>> => {
public send = <M extends Methods, P, R>(method: M, params: P): Promise<SuccessResponse<R>> => {
const request = MessageFormatter.makeRequest(method, params);

if (typeof window === 'undefined') {
throw new Error("Window doesn't exist");
}

window.parent.postMessage(request, '*');
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
this.callbacks.set(request.id, (response: Response<R>) => {
if (!response.success) {
reject(new Error(response.error));
return;
}

resolve(response);
});
});
Expand Down
4 changes: 0 additions & 4 deletions packages/safe-apps-sdk/src/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ class Eth {

const response = await this.communicator.send<Methods.rpcCall, RPCPayload<P>, R>(Methods.rpcCall, payload);

if (!response.success) {
throw new Error(response.error);
}

return response.data;
};
}
Expand Down
8 changes: 0 additions & 8 deletions packages/safe-apps-sdk/src/safe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class Safe {
undefined,
);

if (!response.success) {
throw new Error(response.error);
}

return response.data;
}

Expand All @@ -30,10 +26,6 @@ class Safe {
},
);

if (!response.success) {
throw new Error(response.error);
}

return response.data;
}
}
Expand Down
8 changes: 0 additions & 8 deletions packages/safe-apps-sdk/src/txs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class TXs {
GatewayTransactionDetails
>(Methods.getTxBySafeTxHash, { safeTxHash });

if (!response.success) {
throw new Error(response.error);
}

return response.data;
}

Expand All @@ -48,10 +44,6 @@ class TXs {
SendTransactionsResponse
>(Methods.sendTransactions, messagePayload);

if (!response.success) {
throw new Error(response.error);
}

return response.data;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/safe-apps-sdk/src/types/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export type SuccessResponse<T = MethodToResponse[Methods]> = {
export type Response<T = MethodToResponse[Methods]> = ErrorResponse | SuccessResponse<T>;

export interface Communicator {
send<M extends Methods, P = unknown, R = unknown>(method: M, params: P): Promise<Response<R>>;
send<M extends Methods, P = unknown, R = unknown>(method: M, params: P): Promise<SuccessResponse<R>>;
}

0 comments on commit 97920f8

Please sign in to comment.