diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-01-12 07:58:33 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-01-13 00:01:41 +0800 |
commit | c2c75124315f71c8a87ac83cbab84ba02115169d (patch) | |
tree | 19c187b05e52798f3c42ae423bbf12dbd9a34c13 /packages/connect | |
parent | 97e01d7a427e18d740f692ccc8a65225b9d36268 (diff) | |
download | dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.gz dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.bz2 dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.lz dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.xz dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.zst dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.zip |
Improve HttpClient errors
Diffstat (limited to 'packages/connect')
-rw-r--r-- | packages/connect/CHANGELOG.md | 5 | ||||
-rw-r--r-- | packages/connect/src/http_client.ts | 11 | ||||
-rw-r--r-- | packages/connect/test/http_client_test.ts | 11 |
3 files changed, 22 insertions, 5 deletions
diff --git a/packages/connect/CHANGELOG.md b/packages/connect/CHANGELOG.md index 2d9e2d89f..19d38df5f 100644 --- a/packages/connect/CHANGELOG.md +++ b/packages/connect/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## v0.x.x - _TBD, 2017_ + + * Sanitize api endpoint url and remove trailing slashes (#315) + * Improve error message text in HttpClient (#315) + ## v0.4.0 - _January 11, 2017_ * Prevent getFeesAsync method on HttpClient from mutating input (#296) diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 5604a9607..c30b499a2 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -20,6 +20,7 @@ import { } from './types'; import { relayerResponseJsonParsers } from './utils/relayer_response_json_parsers'; +const TRAILING_SLASHES_REGEX = /\/+$/; /** * This class includes all the functionality related to interacting with a set of HTTP endpoints * that implement the standard relayer API v0 @@ -33,7 +34,7 @@ export class HttpClient implements Client { */ constructor(url: string) { assert.isHttpUrl('url', url); - this._apiEndpointUrl = url; + this._apiEndpointUrl = url.replace(TRAILING_SLASHES_REGEX, ''); // remove trailing slashes } /** * Retrieve token pair info from the API @@ -134,16 +135,18 @@ export class HttpClient implements Client { const headers = new Headers({ 'content-type': 'application/json', }); - const response = await fetch(url, { method: requestType, body: JSON.stringify(payload), headers, }); + const json = await response.json(); if (!response.ok) { - throw Error(response.statusText); + const errorString = `${response.status} - ${response.statusText}\n${requestType} ${url}\n${JSON.stringify( + json, + )}`; + throw Error(errorString); } - const json = await response.json(); return json; } } diff --git a/packages/connect/test/http_client_test.ts b/packages/connect/test/http_client_test.ts index e7096edad..38d00d849 100644 --- a/packages/connect/test/http_client_test.ts +++ b/packages/connect/test/http_client_test.ts @@ -29,6 +29,15 @@ describe('HttpClient', () => { afterEach(() => { fetchMock.restore(); }); + describe('#constructor', () => { + it('should remove trailing slashes from api url', async () => { + const urlWithTrailingSlash = 'https://slash.com/'; + const urlWithoutTrailingSlash = 'https://slash.com'; + const client = new HttpClient(urlWithTrailingSlash); + const sanitizedUrl = (client as any)._apiEndpointUrl; + expect(sanitizedUrl).to.be.deep.equal(urlWithoutTrailingSlash); + }); + }); describe('#getTokenPairsAsync', () => { const url = `${relayUrl}/v0/token_pairs`; it('gets token pairs', async () => { @@ -36,7 +45,7 @@ describe('HttpClient', () => { const tokenPairs = await relayerClient.getTokenPairsAsync(); expect(tokenPairs).to.be.deep.equal(tokenPairsResponse); }); - it('gets specfic token pairs for request', async () => { + it('gets specific token pairs for request', async () => { const tokenAddress = '0x323b5d4c32345ced77393b3530b1eed0f346429d'; const tokenPairsRequest = { tokenA: tokenAddress, |