Skip to content

Commit

Permalink
Merge pull request #3314 from ethereum/pri/error-handling
Browse files Browse the repository at this point in the history
WebsocketProvider - Error Handling
  • Loading branch information
nivida committed Jan 16, 2020
2 parents f937631 + 3ca9b09 commit cd680eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
18 changes: 12 additions & 6 deletions packages/web3-core-helpers/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = {
InvalidNumberOfParams: function (got, expected, method) {
return new Error('Invalid number of parameters for "'+ method +'". Got '+ got +' expected '+ expected +'!');
},
InvalidConnection: function (host){
return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +'.');
InvalidConnection: function (host, event){
return this.ConnectionError('CONNECTION ERROR: Couldn\'t connect to node '+ host +'.', event);
},
InvalidProvider: function () {
return new Error('Provider not set or invalid');
Expand All @@ -44,17 +44,23 @@ module.exports = {
ConnectionTimeout: function (ms){
return new Error('CONNECTION TIMEOUT: timeout of ' + ms + ' ms achived');
},
ConnectionNotOpenError: function (){
return new Error('connection not open on send()');
ConnectionNotOpenError: function (event){
return this.ConnectionError('connection not open on send()', event);
},
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 + '`');
ConnectionError: function (msg, event){
const error = new Error(msg);
if (event) {
error.code = event.code;
error.reason = event.reason;
}

return error;
},
RevertInstructionError: function(reason, signature) {
var error = new Error('Your request got reverted with the following reason string: ' + reason);
Expand Down
14 changes: 12 additions & 2 deletions packages/web3-core-helpers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export class errors {
expected: number,
method: string
): Error;
static InvalidConnection(host: string): Error;
static InvalidConnection(host: string, event?: WebSocketEvent): ConnectionError;
static InvalidProvider(): Error;
static InvalidResponse(result: Error): Error;
static ConnectionTimeout(ms: string): Error;
static ConnectionNotOpenError(): Error;
static MaxAttemptsReachedOnReconnectingError(): Error;
static PendingRequestsOnReconnectingError(): Error;
static ConnectionClosedError(event: {code: number, reason: string}): Error;
static ConnectionError(msg: string, event?: WebSocketEvent): ConnectionError;
static RevertInstructionError(reason: string, signature: string): RevertInstructionError
static TransactionRevertInstructionError(reason: string, signature: string, receipt: object): TransactionRevertInstructionError
static TransactionError(message: string, receipt: object): TransactionError
Expand Down Expand Up @@ -229,3 +229,13 @@ export interface TransactionRevertInstructionError extends Error {
export interface TransactionError extends Error {
receipt: object;
}

export interface ConnectionError extends Error {
code: string | undefined;
reason: string | undefined;
}

export interface WebSocketEvent {
code?: number;
reason?: string;
}
9 changes: 5 additions & 4 deletions packages/web3-core-helpers/types/tests/errors-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
* @date 2019
*/

import { errors } from 'web3-core-helpers';
import { errors, WebSocketEvent } from 'web3-core-helpers';

// $ExpectType Error
errors.ErrorResponse(new Error('hey'));

// $ExpectType Error
errors.InvalidNumberOfParams(1, 3, 'method');

// $ExpectType Error
// $ExpectType ConnectionError
errors.InvalidConnection('https://localhost:2345432');

// $ExpectType Error
Expand All @@ -46,8 +46,9 @@ errors.MaxAttemptsReachedOnReconnectingError();
// $ExpectType Error
errors.PendingRequestsOnReconnectingError();

// $ExpectType Error
errors.ConnectionClosedError({code: 100, reason: 'reason'});
const event: WebSocketEvent = {code: 100, reason: 'reason'};
// $ExpectType ConnectionError
errors.ConnectionError('msg', event);

// $ExpectType RevertInstructionError
errors.RevertInstructionError('reason', 'signature');
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-providers-ws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ WebsocketProvider.prototype._onClose = function (event) {

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

if (this.responseQueue.size > 0) {
this.responseQueue.forEach(function (request, key) {
request.callback(errors.ConnectionClosedError(event));
request.callback(errors.InvalidConnection('on WS', event));
_this.responseQueue.delete(key);
});
}
Expand Down
12 changes: 12 additions & 0 deletions test/websocket.ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe('WebsocketProvider (ganache)', function () {
await web3.eth.getBlockNumber();
assert.fail();
} catch (err) {
assert(err.code, 1006);
assert(err.reason, 'connection failed');
assert(err.message.includes('connection not open on send'));
}
});
Expand All @@ -36,12 +38,17 @@ describe('WebsocketProvider (ganache)', function () {
web3 = new Web3(host + 8777);

try { await web3.eth.getBlockNumber() } catch (err) {
assert(err.message.includes('connection not open on send'));
assert(err.code, 1006);
assert(err.reason, 'connection failed');

try {
await web3.eth.getBlockNumber();
assert.fail();
} catch (err){
assert(err.message.includes('connection not open on send'));
assert(typeof err.code === 'undefined');
assert(typeof err.reason === 'undefined');
}
}
});
Expand All @@ -61,6 +68,8 @@ describe('WebsocketProvider (ganache)', function () {
assert.fail();
} catch(err){
assert(err.message.includes('connection not open on send'));
assert(typeof err.code === 'undefined');
assert(typeof err.reason === 'undefined');
}
});

Expand Down Expand Up @@ -229,6 +238,9 @@ describe('WebsocketProvider (ganache)', function () {
} catch (err) {
await pify(server.close)();
assert(err.message.includes('connection not open on send'));
assert(typeof err.code === 'undefined');
assert(typeof err.reason === 'undefined');

resolve();
}
});
Expand Down

0 comments on commit cd680eb

Please sign in to comment.