Skip to content

Commit

Permalink
error listener removed in WSProvider because the close listener does …
Browse files Browse the repository at this point in the history
…get triggered anyways, error handling immproved in WebsocketProvider and RequestManager
  • Loading branch information
nivida committed Jan 9, 2020
1 parent 3048f50 commit d7fb27f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 54 deletions.
14 changes: 13 additions & 1 deletion packages/web3-core-helpers/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,21 @@ module.exports = {
var message = !!result && !!result.error && !!result.error.message ? result.error.message : 'Invalid JSON RPC response: ' + JSON.stringify(result);
return new Error(message);
},
ConnectionTimeout: function (ms){
ConnectionTimeoutError: function (ms){
return new Error('CONNECTION TIMEOUT: timeout of ' + ms + ' ms achived');
},
ConnectionNotOpenError: function (){
return new Error('connection not open on send()');
},
MaxAttemptsReachedOnReconnectingError: function (){
return new Error('Maximum number of reconnect attempts reached!');
},
PendingRequestsOnReconnectingError: function (){
return new Error('CONNECTION ERROR: Provider started to reconnect before the response got received!');
},
ConnectionClosedError: function (event){
return new Error('CONNECTION ERROR: The connection got closed with close code `' + event.code + '` and the following reason string `' + event.reason + '`');
},
RevertInstructionError: function(reason, signature) {
var error = new Error('Your request got reverted with the following reason string: ' + reason);
error.reason = reason;
Expand Down
7 changes: 0 additions & 7 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ RequestManager.prototype.setProvider = function (provider, net) {
});
});

// notify all subscriptions about the error condition
this.provider.on('error', function error(error) {
_this.subscriptions.forEach(function (subscription) {
subscription.callback(error);
});
});

// notify all subscriptions about the close condition
this.provider.on('close', function close(event) {
_this.subscriptions.forEach(function (subscription) {
Expand Down
56 changes: 10 additions & 46 deletions packages/web3-providers-ws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

var EventEmitter = require('eventemitter3');
var helpers = require('./helpers.js');
var errors = require('web3-core-helpers').errors;
var Ws = require('@web3-js/websocket').w3cwebsocket;

/**
Expand Down Expand Up @@ -135,38 +136,6 @@ WebsocketProvider.prototype._onMessage = function (e) {
});
};

/**
* Listener for the `error` event of the underlying WebSocket object
*
* @method _onError
*
* @returns {void}
*/
WebsocketProvider.prototype._onError = function (error) {
if (!error.code && !this.reconnecting) {
var _this = this;

this.emit(this.ERROR, error);

if (this.requestQueue.size > 0) {
this.requestQueue.forEach(function (request, key) {
request.callback(error);
_this.requestQueue.delete(key);
});
}

if (this.responseQueue.size > 0) {
this.responseQueue.forEach(function (request, key) {
request.callback(error);
_this.responseQueue.delete(key);
});
}

this._removeSocketListeners();
this.removeAllListeners();
}
};

/**
* Listener for the `open` event of the underlying WebSocket object
*
Expand Down Expand Up @@ -206,17 +175,18 @@ WebsocketProvider.prototype._onClose = function (event) {
}

this.emit(this.CLOSE, event);
this.emit(this.ERROR, event);

if (this.requestQueue.size > 0) {
this.requestQueue.forEach(function (request, key) {
request.callback(new Error('CONNECTION ERROR: The connection got closed with close code `' + event.code + '` and the following reason string `' + event.reason + '`'));
request.callback(errors.ConnectionClosedError(event));
_this.requestQueue.delete(key);
});
}

if (this.responseQueue.size > 0) {
this.responseQueue.forEach(function (request, key) {
request.callback(new Error('CONNECTION ERROR: The connection got closed with close code `' + event.code + '` and the following reason string `' + event.reason + '`'));
request.callback(errors.ConnectionClosedError(event));
_this.responseQueue.delete(key);
});
}
Expand All @@ -236,7 +206,6 @@ WebsocketProvider.prototype._addSocketListeners = function () {
this.connection.addEventListener('message', this._onMessage.bind(this));
this.connection.addEventListener('open', this._onConnect.bind(this));
this.connection.addEventListener('close', this._onClose.bind(this));
this.connection.addEventListener('error', this._onError.bind(this));
};

/**
Expand All @@ -250,7 +219,6 @@ WebsocketProvider.prototype._removeSocketListeners = function () {
this.connection.removeEventListener('message', this._onMessage);
this.connection.removeEventListener('open', this._onConnect);
this.connection.removeEventListener('close', this._onClose);
this.connection.removeEventListener('error', this._onError);
};

/**
Expand Down Expand Up @@ -298,13 +266,11 @@ WebsocketProvider.prototype._parseResponse = function (data) {
return;
}

var error = new Error('Connection error: Timeout exceeded');

_this.emit(_this.ERROR, error);
_this.emit(_this.ERROR, errors.ConnectionTimeoutError(_this._customTimeout));

if (_this.requestQueue.size > 0) {
_this.requestQueue.forEach(function (request, key) {
request.callback(error);
request.callback(errors.ConnectionTimeoutError(_this._customTimeout));
_this.requestQueue.delete(key);
});
}
Expand Down Expand Up @@ -352,10 +318,8 @@ WebsocketProvider.prototype.send = function (payload, callback) {
if (this.connection.readyState !== this.connection.OPEN) {
this.requestQueue.delete(id);

var error = new Error('connection not open on send()');

this.emit(this.ERROR, error);
request.callback(error);
this.emit(this.ERROR, errors.ConnectionNotOpenError());
request.callback(errors.ConnectionNotOpenError());

return;
}
Expand Down Expand Up @@ -427,7 +391,7 @@ WebsocketProvider.prototype.reconnect = function () {

if (this.responseQueue.size > 0) {
this.responseQueue.forEach(function (request, key) {
request.callback(new Error('CONNECTION ERROR: Provider started to reconnect before the response got received!'));
request.callback(errors.PendingRequestsOnReconnectingError());
_this.responseQueue.delete(key);
});
}
Expand All @@ -446,7 +410,7 @@ WebsocketProvider.prototype.reconnect = function () {
return;
}

this.emit(this.ERROR, new Error('Maximum number of reconnect attempts reached!'));
this.emit(this.ERROR, errors.MaxAttemptsReachedOnReconnectingError());
};

module.exports = WebsocketProvider;

0 comments on commit d7fb27f

Please sign in to comment.