Merge pull request #318 from 0xProject/feature/httpClientErrorImprovements
Improve HttpClient errors
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
# CHANGELOG
|
# CHANGELOG
|
||||||
|
|
||||||
|
## v0.x.x - _TBD, 2017_
|
||||||
|
|
||||||
|
* Sanitize api endpoint url and remove trailing slashes (#318)
|
||||||
|
* Improve error message text in HttpClient (#318)
|
||||||
|
* Stop appending '/v0' to api endpoint url in HttpClient (#318)
|
||||||
|
|
||||||
## v0.4.0 - _January 11, 2017_
|
## v0.4.0 - _January 11, 2017_
|
||||||
|
|
||||||
* Prevent getFeesAsync method on HttpClient from mutating input (#296)
|
* Prevent getFeesAsync method on HttpClient from mutating input (#296)
|
||||||
|
@@ -20,6 +20,7 @@ import {
|
|||||||
} from './types';
|
} from './types';
|
||||||
import { relayerResponseJsonParsers } from './utils/relayer_response_json_parsers';
|
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
|
* This class includes all the functionality related to interacting with a set of HTTP endpoints
|
||||||
* that implement the standard relayer API v0
|
* that implement the standard relayer API v0
|
||||||
@@ -33,7 +34,7 @@ export class HttpClient implements Client {
|
|||||||
*/
|
*/
|
||||||
constructor(url: string) {
|
constructor(url: string) {
|
||||||
assert.isHttpUrl('url', url);
|
assert.isHttpUrl('url', url);
|
||||||
this._apiEndpointUrl = url;
|
this._apiEndpointUrl = url.replace(TRAILING_SLASHES_REGEX, ''); // remove trailing slashes
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Retrieve token pair info from the API
|
* Retrieve token pair info from the API
|
||||||
@@ -130,20 +131,22 @@ export class HttpClient implements Client {
|
|||||||
const stringifiedParams = queryString.stringify(params);
|
const stringifiedParams = queryString.stringify(params);
|
||||||
query = `?${stringifiedParams}`;
|
query = `?${stringifiedParams}`;
|
||||||
}
|
}
|
||||||
const url = `${this._apiEndpointUrl}/v0${path}${query}`;
|
const url = `${this._apiEndpointUrl}${path}${query}`;
|
||||||
const headers = new Headers({
|
const headers = new Headers({
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: requestType,
|
method: requestType,
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
headers,
|
headers,
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
|
||||||
throw Error(response.statusText);
|
|
||||||
}
|
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorString = `${response.status} - ${response.statusText}\n${requestType} ${url}\n${JSON.stringify(
|
||||||
|
json,
|
||||||
|
)}`;
|
||||||
|
throw Error(errorString);
|
||||||
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -29,14 +29,23 @@ describe('HttpClient', () => {
|
|||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
fetchMock.restore();
|
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', () => {
|
describe('#getTokenPairsAsync', () => {
|
||||||
const url = `${relayUrl}/v0/token_pairs`;
|
const url = `${relayUrl}/token_pairs`;
|
||||||
it('gets token pairs', async () => {
|
it('gets token pairs', async () => {
|
||||||
fetchMock.get(url, tokenPairsResponseJSON);
|
fetchMock.get(url, tokenPairsResponseJSON);
|
||||||
const tokenPairs = await relayerClient.getTokenPairsAsync();
|
const tokenPairs = await relayerClient.getTokenPairsAsync();
|
||||||
expect(tokenPairs).to.be.deep.equal(tokenPairsResponse);
|
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 tokenAddress = '0x323b5d4c32345ced77393b3530b1eed0f346429d';
|
||||||
const tokenPairsRequest = {
|
const tokenPairsRequest = {
|
||||||
tokenA: tokenAddress,
|
tokenA: tokenAddress,
|
||||||
@@ -52,7 +61,7 @@ describe('HttpClient', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#getOrdersAsync', () => {
|
describe('#getOrdersAsync', () => {
|
||||||
const url = `${relayUrl}/v0/orders`;
|
const url = `${relayUrl}/orders`;
|
||||||
it('gets orders', async () => {
|
it('gets orders', async () => {
|
||||||
fetchMock.get(url, ordersResponseJSON);
|
fetchMock.get(url, ordersResponseJSON);
|
||||||
const orders = await relayerClient.getOrdersAsync();
|
const orders = await relayerClient.getOrdersAsync();
|
||||||
@@ -75,7 +84,7 @@ describe('HttpClient', () => {
|
|||||||
});
|
});
|
||||||
describe('#getOrderAsync', () => {
|
describe('#getOrderAsync', () => {
|
||||||
const orderHash = '0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
|
const orderHash = '0xabc67323774bdbd24d94f977fa9ac94a50f016026fd13f42990861238897721f';
|
||||||
const url = `${relayUrl}/v0/order/${orderHash}`;
|
const url = `${relayUrl}/order/${orderHash}`;
|
||||||
it('gets order', async () => {
|
it('gets order', async () => {
|
||||||
fetchMock.get(url, orderResponseJSON);
|
fetchMock.get(url, orderResponseJSON);
|
||||||
const order = await relayerClient.getOrderAsync(orderHash);
|
const order = await relayerClient.getOrderAsync(orderHash);
|
||||||
@@ -91,7 +100,7 @@ describe('HttpClient', () => {
|
|||||||
baseTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
baseTokenAddress: '0x323b5d4c32345ced77393b3530b1eed0f346429d',
|
||||||
quoteTokenAddress: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
quoteTokenAddress: '0xa2b31dacf30a9c50ca473337c01d8a201ae33e32',
|
||||||
};
|
};
|
||||||
const url = `${relayUrl}/v0/orderbook?baseTokenAddress=${request.baseTokenAddress}"eTokenAddress=${
|
const url = `${relayUrl}/orderbook?baseTokenAddress=${request.baseTokenAddress}"eTokenAddress=${
|
||||||
request.quoteTokenAddress
|
request.quoteTokenAddress
|
||||||
}`;
|
}`;
|
||||||
it('gets order book', async () => {
|
it('gets order book', async () => {
|
||||||
@@ -116,7 +125,7 @@ describe('HttpClient', () => {
|
|||||||
salt: new BigNumber('256'),
|
salt: new BigNumber('256'),
|
||||||
expirationUnixTimestampSec: new BigNumber('42'),
|
expirationUnixTimestampSec: new BigNumber('42'),
|
||||||
};
|
};
|
||||||
const url = `${relayUrl}/v0/fees`;
|
const url = `${relayUrl}/fees`;
|
||||||
it('gets fees', async () => {
|
it('gets fees', async () => {
|
||||||
fetchMock.post(url, feesResponseJSON);
|
fetchMock.post(url, feesResponseJSON);
|
||||||
const fees = await relayerClient.getFeesAsync(request);
|
const fees = await relayerClient.getFeesAsync(request);
|
||||||
|
11
yarn.lock
11
yarn.lock
@@ -595,7 +595,7 @@ async-eventemitter@^0.2.2:
|
|||||||
dependencies:
|
dependencies:
|
||||||
async "^2.4.0"
|
async "^2.4.0"
|
||||||
|
|
||||||
"async-eventemitter@github:ahultgren/async-eventemitter#fa06e39e56786ba541c180061dbf2c0a5bbf951c":
|
async-eventemitter@ahultgren/async-eventemitter#fa06e39e56786ba541c180061dbf2c0a5bbf951c:
|
||||||
version "0.2.3"
|
version "0.2.3"
|
||||||
resolved "https://codeload.github.com/ahultgren/async-eventemitter/tar.gz/fa06e39e56786ba541c180061dbf2c0a5bbf951c"
|
resolved "https://codeload.github.com/ahultgren/async-eventemitter/tar.gz/fa06e39e56786ba541c180061dbf2c0a5bbf951c"
|
||||||
dependencies:
|
dependencies:
|
||||||
@@ -1287,14 +1287,14 @@ bignumber.js@^4.0.2, bignumber.js@~4.1.0:
|
|||||||
version "4.1.0"
|
version "4.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
|
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
|
||||||
|
|
||||||
|
"bignumber.js@git+https://github.com/debris/bignumber.js#master":
|
||||||
|
version "2.0.7"
|
||||||
|
resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9"
|
||||||
|
|
||||||
"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
|
"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
|
||||||
version "2.0.7"
|
version "2.0.7"
|
||||||
resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
|
resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
|
||||||
|
|
||||||
"bignumber.js@git+https://github.com/debris/bignumber.js.git#master":
|
|
||||||
version "2.0.7"
|
|
||||||
resolved "git+https://github.com/debris/bignumber.js.git#c7a38de919ed75e6fb6ba38051986e294b328df9"
|
|
||||||
|
|
||||||
"bignumber.js@git+https://github.com/frozeman/bignumber.js-nolookahead.git":
|
"bignumber.js@git+https://github.com/frozeman/bignumber.js-nolookahead.git":
|
||||||
version "2.0.7"
|
version "2.0.7"
|
||||||
resolved "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934"
|
resolved "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934"
|
||||||
@@ -9241,7 +9241,6 @@ types-ethereumjs-util@0xProject/types-ethereumjs-util:
|
|||||||
|
|
||||||
types-ethereumjs-util@0xproject/types-ethereumjs-util:
|
types-ethereumjs-util@0xproject/types-ethereumjs-util:
|
||||||
version "0.0.6"
|
version "0.0.6"
|
||||||
uid "0c5058e4f9d6585f7c7c1f88939546acb14323b8"
|
|
||||||
resolved "https://codeload.github.com/0xproject/types-ethereumjs-util/tar.gz/0c5058e4f9d6585f7c7c1f88939546acb14323b8"
|
resolved "https://codeload.github.com/0xproject/types-ethereumjs-util/tar.gz/0c5058e4f9d6585f7c7c1f88939546acb14323b8"
|
||||||
dependencies:
|
dependencies:
|
||||||
bn.js "^4.11.7"
|
bn.js "^4.11.7"
|
||||||
|
Reference in New Issue
Block a user