Skip to content

Commit

Permalink
PP-682/review error handler (#79)
Browse files Browse the repository at this point in the history
* feat: handle errors from server
  • Loading branch information
ironFe93 authored Mar 10, 2023
1 parent 1b3101f commit a11522b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/api/common/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type RelayPath = (typeof PATHS)[keyof typeof PATHS];
type SignedTransactionDetails = {
transactionHash: string;
signedTx: string;
error?: string;
message?: string;
};

export function buildUrl(base: string, path: string, verifier = ''): string {
Expand Down Expand Up @@ -86,13 +88,17 @@ class HttpClient {
): Promise<string> {
const url = buildUrl(relayUrl, PATHS.POST_RELAY_REQUEST);

const { signedTx } =
const { signedTx, error } =
await this._httpWrapper.sendPromise<SignedTransactionDetails>(
url,
this._stringifyEnvelopingTx(envelopingTx)
);
log.info('relayTransaction response:', signedTx);

if (error) {
throw new Error(`Got error response from relay: ${error}`);
}

if (!signedTx) {
throw new Error(
'Got invalid response from relay: signedTx field missing.'
Expand Down
13 changes: 13 additions & 0 deletions test/api/common/HttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ describe('HttpClient', function () {
httpClient.relayTransaction(fakeURL, request)
).to.be.rejectedWith(expectedError);
});

it('should throw error if response object contains property `error`', async function () {
const serverError = 'error message from server';

const expectedError = `Got error response from relay: ${serverError}`;
sandbox
.stub(HttpWrapper.prototype, 'sendPromise')
.resolves({ error: serverError });

await expect(
httpClient.relayTransaction(fakeURL, request)
).to.be.rejectedWith(expectedError);
});
});

describe('estimateMaxPossibleGas', function () {
Expand Down

0 comments on commit a11522b

Please sign in to comment.