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 6adb739ac7e..77194695fbb 100644 --- a/packages/web3-providers/src/providers/HttpProvider.js +++ b/packages/web3-providers/src/providers/HttpProvider.js @@ -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}`)); } } }; @@ -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; + } } diff --git a/packages/web3-providers/tests/src/providers/HttpProviderTest.js b/packages/web3-providers/tests/src/providers/HttpProviderTest.js index 7e216b79cbc..6ee355fb13b 100644 --- a/packages/web3-providers/tests/src/providers/HttpProviderTest.js +++ b/packages/web3-providers/tests/src/providers/HttpProviderTest.js @@ -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];