aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-01-12 07:58:33 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-01-13 00:01:41 +0800
commitc2c75124315f71c8a87ac83cbab84ba02115169d (patch)
tree19c187b05e52798f3c42ae423bbf12dbd9a34c13
parent97e01d7a427e18d740f692ccc8a65225b9d36268 (diff)
downloaddexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.gz
dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.tar.zst
dexon-0x-contracts-c2c75124315f71c8a87ac83cbab84ba02115169d.zip
Improve HttpClient errors
-rw-r--r--packages/connect/CHANGELOG.md5
-rw-r--r--packages/connect/src/http_client.ts11
-rw-r--r--packages/connect/test/http_client_test.ts11
-rw-r--r--yarn.lock11
4 files changed, 27 insertions, 11 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,
diff --git a/yarn.lock b/yarn.lock
index 72657000f..9c433625d 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -595,7 +595,7 @@ async-eventemitter@^0.2.2:
dependencies:
async "^2.4.0"
-"async-eventemitter@github:ahultgren/async-eventemitter#fa06e39e56786ba541c180061dbf2c0a5bbf951c":
+async-eventemitter@ahultgren/async-eventemitter#fa06e39e56786ba541c180061dbf2c0a5bbf951c:
version "0.2.3"
resolved "https://codeload.github.com/ahultgren/async-eventemitter/tar.gz/fa06e39e56786ba541c180061dbf2c0a5bbf951c"
dependencies:
@@ -1287,13 +1287,13 @@ bignumber.js@^4.0.2, bignumber.js@~4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
-"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
+"bignumber.js@git+https://github.com/debris/bignumber.js#master":
version "2.0.7"
- resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
+ resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9"
-"bignumber.js@git+https://github.com/debris/bignumber.js.git#master":
+"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
version "2.0.7"
- resolved "git+https://github.com/debris/bignumber.js.git#c7a38de919ed75e6fb6ba38051986e294b328df9"
+ resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
"bignumber.js@git+https://github.com/frozeman/bignumber.js-nolookahead.git":
version "2.0.7"
@@ -9241,7 +9241,6 @@ types-ethereumjs-util@0xProject/types-ethereumjs-util:
types-ethereumjs-util@0xproject/types-ethereumjs-util:
version "0.0.6"
- uid "0c5058e4f9d6585f7c7c1f88939546acb14323b8"
resolved "https://codeload.github.com/0xproject/types-ethereumjs-util/tar.gz/0c5058e4f9d6585f7c7c1f88939546acb14323b8"
dependencies:
bn.js "^4.11.7"
* Update to 3.5.0.novel2011-08-213-28/+41 * Augment port description.danfe2011-04-011-9/+20 * - Replace direct (via LIB_DEPENDS) libXft dependency with USE_XORG onedanfe2011-04-011-10/+6 * - Get Rid MD5 supportmiwi2011-03-201-1/+0 * Sync to new bsd.autotools.mkade2010-12-041-1/+1 * Bounce PORTREVISION for gettext-related ports. Have fun, ya'll.ade2010-05-311-0/+1 * Upgrade to version 3.4.11.2:dougb2010-05-252-4/+4 * - Update to 3.4.11.1 [1]novel2010-04-233-18/+19 * - update to 1.4.1dinoex2010-03-281-0/+1 * Upgrade x11-wm/openbox to version 3.4.11dougb2010-02-233-22/+19 * - Update to 3.4.10novel2010-01-133-9/+10 * Update to 3.4.9.novel2009-12-313-6/+7 * I checked for plist problems without enabling gettextdougb2009-12-161-0/+8 * Update to version 3.4.8 which fixes numerous bugs. Full details on whatdougb2009-12-163-7/+10 * Add mirrors to MASTER_SITES.novel2009-11-221-1/+3 * -Repocopy devel/libtool15 -> libtool22 and libltdl15 -> libltdl22.mezz2009-08-031-1/+1 * Update to 3.4.7.2.novel2008-06-183-7/+9 * Bump portrevision due to upgrade of devel/gettext.edwin2008-06-06