Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpProvider error handling improved #2414

Merged
merged 5 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('TransactionReceiptValidatorTest', () => {

it('calls validate and returns true with undefined status property', () => {
delete receipt.status;

Utils.hexToNumber.mockReturnValueOnce(110);

method.parameters = [
Expand Down
1 change: 0 additions & 1 deletion packages/web3-core-subscriptions/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,3 @@ export SyncingSubscription from './subscriptions/eth/SyncingSubscription';

// Shh
export MessagesSubscription from './subscriptions/shh/MessagesSubscription';

29 changes: 24 additions & 5 deletions packages/web3-providers/src/providers/HttpProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,17 @@ export default class HttpProvider {
this.connected = true;
}

if (request.readyState === 4 && request.status === 200) {
try {
return resolve(JSON.parse(request.responseText));
} catch (error) {
reject(new Error(`Invalid JSON as response: ${request.responseText}`));
if (request.readyState === 4) {
if (request.status === 200) {
try {
return resolve(JSON.parse(request.responseText));
} catch (error) {
reject(new Error(`Invalid JSON as response: ${request.responseText}`));
}
}

if (this.isInvalidHttpEndpoint(request)) {
reject(new Error(`Connection refused or URL couldn't be resolved: ${this.host}`));
}
}
};
Expand All @@ -170,4 +176,17 @@ export default class HttpProvider {
}
});
}

/**
* Checks if the error `net::ERR_NAME_NOT_RESOLVED` or `net::ERR_CONNECTION_REFUSED` will appear.
*
* @method isInvalidHttpEndpoint
*
* @param {Object} request
*
* @returns {Boolean}
*/
isInvalidHttpEndpoint(request) {
return request.response === null && request.status === 0;
}
}
28 changes: 28 additions & 0 deletions packages/web3-providers/tests/src/providers/HttpProviderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ describe('HttpProviderTest', () => {
expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
});

it('calls sendPayload and returns with a rejected promise because of an not existing http endpoint', async () => {
new XHR();
const xhrMock = XHR.mock.instances[0];

xhrMock.readyState = 4;
xhrMock.status = 0;
xhrMock.response = null;

providersModuleFactoryMock.createXMLHttpRequest.mockReturnValueOnce(xhrMock);

setTimeout(() => {
xhrMock.onreadystatechange();
}, 1);

await expect(httpProvider.sendPayload({id: '0x0'})).rejects.toThrow(
`Connection refused or URL couldn't be resolved: ${httpProvider.host}`
);

expect(providersModuleFactoryMock.createXMLHttpRequest).toHaveBeenCalledWith(
httpProvider.host,
httpProvider.timeout,
httpProvider.headers,
httpProvider.agent
);

expect(xhrMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
});

it('calls sendPayload and returns with a rejected promise because of the exceeded timeout', async () => {
new XHR();
const xhrMock = XHR.mock.instances[0];
Expand Down