From 428dbdec0438d8426af33a5b7c4971d153ca226b Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Mon, 25 Feb 2019 12:38:36 +0100 Subject: [PATCH 1/4] The HttpProvider can now handle the 'net::ERR_NAME_NOT_RESOLVED' and 'net::ERR_CONNECTION_REFUSED' error --- .../src/providers/HttpProvider.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/web3-providers/src/providers/HttpProvider.js b/packages/web3-providers/src/providers/HttpProvider.js index 6adb739ac7e..6ab1b60c53a 100644 --- a/packages/web3-providers/src/providers/HttpProvider.js +++ b/packages/web3-providers/src/providers/HttpProvider.js @@ -140,15 +140,25 @@ export default class HttpProvider { ); request.onreadystatechange = () => { + if (request.response === null && request.status === 0 && ) { + + } + if (request.readyState !== 0 && request.readyState !== 1) { 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 (request.response === null && request.status === 0) { + reject(new Error(`Connection refused or URL couldn\'t be resolved: ${this.host}`)) } } }; From 29e5943286bdd6a936107b6a32d9ea2b22b26203 Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Tue, 26 Feb 2019 09:09:15 +0100 Subject: [PATCH 2/4] HttpProvider fixed and test updated --- .../src/providers/HttpProvider.js | 4 --- .../tests/src/providers/HttpProviderTest.js | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/web3-providers/src/providers/HttpProvider.js b/packages/web3-providers/src/providers/HttpProvider.js index 6ab1b60c53a..7fdb2ce386f 100644 --- a/packages/web3-providers/src/providers/HttpProvider.js +++ b/packages/web3-providers/src/providers/HttpProvider.js @@ -140,10 +140,6 @@ export default class HttpProvider { ); request.onreadystatechange = () => { - if (request.response === null && request.status === 0 && ) { - - } - if (request.readyState !== 0 && request.readyState !== 1) { this.connected = true; } diff --git a/packages/web3-providers/tests/src/providers/HttpProviderTest.js b/packages/web3-providers/tests/src/providers/HttpProviderTest.js index 7e216b79cbc..fd1a7d0042f 100644 --- a/packages/web3-providers/tests/src/providers/HttpProviderTest.js +++ b/packages/web3-providers/tests/src/providers/HttpProviderTest.js @@ -225,6 +225,32 @@ 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]; From 46f3017ea6a9d7ec14025331e966b9030196b797 Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Tue, 26 Feb 2019 09:13:42 +0100 Subject: [PATCH 3/4] eslint executed --- .../tests/src/validators/TransactionReceiptValidatorTest.js | 2 +- packages/web3-core-subscriptions/src/index.js | 1 - packages/web3-providers/src/providers/HttpProvider.js | 2 +- .../web3-providers/tests/src/providers/HttpProviderTest.js | 4 +++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/web3-core-method/tests/src/validators/TransactionReceiptValidatorTest.js b/packages/web3-core-method/tests/src/validators/TransactionReceiptValidatorTest.js index 0769d128b9e..99ae5cdc12b 100644 --- a/packages/web3-core-method/tests/src/validators/TransactionReceiptValidatorTest.js +++ b/packages/web3-core-method/tests/src/validators/TransactionReceiptValidatorTest.js @@ -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 = [ diff --git a/packages/web3-core-subscriptions/src/index.js b/packages/web3-core-subscriptions/src/index.js index a9d4c232bac..737904a13e8 100644 --- a/packages/web3-core-subscriptions/src/index.js +++ b/packages/web3-core-subscriptions/src/index.js @@ -45,4 +45,3 @@ export SyncingSubscription from './subscriptions/eth/SyncingSubscription'; // Shh export MessagesSubscription from './subscriptions/shh/MessagesSubscription'; - diff --git a/packages/web3-providers/src/providers/HttpProvider.js b/packages/web3-providers/src/providers/HttpProvider.js index 7fdb2ce386f..502e21bf3dc 100644 --- a/packages/web3-providers/src/providers/HttpProvider.js +++ b/packages/web3-providers/src/providers/HttpProvider.js @@ -154,7 +154,7 @@ export default class HttpProvider { } if (request.response === null && request.status === 0) { - reject(new Error(`Connection refused or URL couldn\'t be resolved: ${this.host}`)) + reject(new Error(`Connection refused or URL couldn't be resolved: ${this.host}`)); } } }; diff --git a/packages/web3-providers/tests/src/providers/HttpProviderTest.js b/packages/web3-providers/tests/src/providers/HttpProviderTest.js index fd1a7d0042f..6ee355fb13b 100644 --- a/packages/web3-providers/tests/src/providers/HttpProviderTest.js +++ b/packages/web3-providers/tests/src/providers/HttpProviderTest.js @@ -239,7 +239,9 @@ describe('HttpProviderTest', () => { xhrMock.onreadystatechange(); }, 1); - await expect(httpProvider.sendPayload({id: '0x0'})).rejects.toThrow(`Connection refused or URL couldn\'t be resolved: ${httpProvider.host}`); + await expect(httpProvider.sendPayload({id: '0x0'})).rejects.toThrow( + `Connection refused or URL couldn't be resolved: ${httpProvider.host}` + ); expect(providersModuleFactoryMock.createXMLHttpRequest).toHaveBeenCalledWith( httpProvider.host, From bed7b7eacb747465fe2ca76757ac79df08d53aa5 Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Tue, 26 Feb 2019 09:18:33 +0100 Subject: [PATCH 4/4] isInvalidHttpEndpoint added to HttpProvider for a better readability --- .../web3-providers/src/providers/HttpProvider.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/web3-providers/src/providers/HttpProvider.js b/packages/web3-providers/src/providers/HttpProvider.js index 502e21bf3dc..77194695fbb 100644 --- a/packages/web3-providers/src/providers/HttpProvider.js +++ b/packages/web3-providers/src/providers/HttpProvider.js @@ -153,7 +153,7 @@ export default class HttpProvider { } } - if (request.response === null && request.status === 0) { + if (this.isInvalidHttpEndpoint(request)) { reject(new Error(`Connection refused or URL couldn't be resolved: ${this.host}`)); } } @@ -176,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; + } }