From 7a9c2bb9c3e69f148548e4f1026f5e4bf63d57bd Mon Sep 17 00:00:00 2001 From: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Thu, 9 Mar 2023 19:43:37 +0100 Subject: [PATCH] Use `MaxAttemptsReachedOnReconnectingError` similar to v1.x (#5894) * use `MaxAttemptsReachedOnReconnectingError` similar to v1.x * update CHANGELOG.md files --- .../providers_migration_guide.md | 16 +++++++++----- .../docs/guides/web3_providers_guide/index.md | 22 +++++++++++-------- packages/web3-errors/CHANGELOG.md | 1 + .../src/errors/connection_errors.ts | 4 ++-- .../unit/__snapshots__/errors.test.ts.snap | 2 +- packages/web3-errors/test/unit/errors.test.ts | 2 +- .../test/integration/reconnection.test.ts | 4 ++-- .../test/integration/reconnection.test.ts | 12 +++++----- packages/web3-utils/src/socket_provider.ts | 7 ++++-- 9 files changed, 42 insertions(+), 28 deletions(-) diff --git a/docs/docs/guides/web3_migration_guide/providers_migration_guide.md b/docs/docs/guides/web3_migration_guide/providers_migration_guide.md index 7cc50878c15..dbedccdc94c 100644 --- a/docs/docs/guides/web3_migration_guide/providers_migration_guide.md +++ b/docs/docs/guides/web3_migration_guide/providers_migration_guide.md @@ -165,21 +165,25 @@ const reconnectOptions: ReconnectOptions = { }; ``` -##### Error message for reconnect attempts +#### Error message for reconnect attempts + +:::note +This section applies for both `IpcProvider` and `WebSocketProvider`. +::: The error in, version 1.x, was an Error object that contains the message: `'Maximum number of reconnect attempts reached!'` -However, the error, in version 4.x, is just an error message (not wrapped in an Error object). And the error message will contain the value of the variable `maxAttempts` as follows: +And, the error in version 4.x, is the same, but will also contain the value of the variable `maxAttempts` as follows: -`` `Max connection attempts exceeded (${maxAttempts})` `` +`` `Maximum number of reconnect attempts reached! (${maxAttempts})` `` And here is how to catch the error, in version 4.x, if max attempts reached when there is auto reconnecting: ```ts -provider.on('error', errorMessage => { - if (errorMessage.startsWith('Max connection attempts exceeded')) { - // the `errorMessage` will be `Max connection attempts exceeded (${maxAttempts})` +provider.on('error', error => { + if (error.message.startsWith('Maximum number of reconnect attempts reached!')) { + // the `error.message` will be `Maximum number of reconnect attempts reached! (${maxAttempts})` // the `maxAttempts` is equal to the provided value by the user, or the default value `5`. } }); diff --git a/docs/docs/guides/web3_providers_guide/index.md b/docs/docs/guides/web3_providers_guide/index.md index c8d5f8ffe5b..97a803bab4c 100644 --- a/docs/docs/guides/web3_providers_guide/index.md +++ b/docs/docs/guides/web3_providers_guide/index.md @@ -159,8 +159,8 @@ Here is how to catch the error if max attempts reached when the auto reconnectin ```ts provider.on('error', errorMessage => { - if (errorMessage.startsWith('Max connection attempts exceeded')) { - // the `errorMessage` will be `Max connection attempts exceeded (${maxAttempts})` + if (errorMessage.startsWith('Maximum number of reconnect attempts reached!')) { + // the `errorMessage` will be `Maximum number of reconnect attempts reached! (${maxAttempts})` // the `maxAttempts` is equal to the provided value by the user or the default `5`. } }); @@ -188,19 +188,23 @@ const reconnectOptions: ReconnectOptions = { }; ``` -##### Error message for reconnect attempts +#### Error message for reconnect attempts -The error message (not wrapped in an Error object) for the max reconnect attempts, will contain the value of the variable `maxAttempts` as follows: +:::note +This section applies for both `IpcProvider` and `WebSocketProvider`. +::: + +The error message, for the max reconnect attempts, will contain the value of the variable `maxAttempts` as follows: -`` `Max connection attempts exceeded (${maxAttempts})` `` +`` `Maximum number of reconnect attempts reached! (${maxAttempts})` `` And here is how to catch the error, if max attempts reached when there is auto reconnecting: ```ts -provider.on('error', errorMessage => { - if (errorMessage.startsWith('Max connection attempts exceeded')) { - // the `errorMessage` will be `Max connection attempts exceeded (${maxAttempts})` - // the `maxAttempts` is equal to the provided value by the user or the default `5`. +provider.on('error', error => { + if (error.message.startsWith('Maximum number of reconnect attempts reached!')) { + // the `error.message` will be `Maximum number of reconnect attempts reached! (${maxAttempts})` + // the `maxAttempts` is equal to the provided value by the user, or the default value `5`. } }); ``` diff --git a/packages/web3-errors/CHANGELOG.md b/packages/web3-errors/CHANGELOG.md index 0bb19126626..06068537093 100644 --- a/packages/web3-errors/CHANGELOG.md +++ b/packages/web3-errors/CHANGELOG.md @@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - The abstract class `Web3Error` is renamed to `BaseWeb3Error` (#5771) +- Using `MaxAttemptsReachedOnReconnectingError` with the same message for 1.x but also adding the `maxAttempts` (#5894) ### Added diff --git a/packages/web3-errors/src/errors/connection_errors.ts b/packages/web3-errors/src/errors/connection_errors.ts index aac82bfaa72..20d41e5ae87 100644 --- a/packages/web3-errors/src/errors/connection_errors.ts +++ b/packages/web3-errors/src/errors/connection_errors.ts @@ -91,8 +91,8 @@ export class ConnectionCloseError extends ConnectionError { } export class MaxAttemptsReachedOnReconnectingError extends ConnectionError { - public constructor() { - super('Maximum number of reconnect attempts reached!'); + public constructor(numberOfAttempts: number) { + super(`Maximum number of reconnect attempts reached! (${numberOfAttempts})`); this.code = ERR_CONN_MAX_ATTEMPTS; } } diff --git a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap index 9e6705c7e85..e53b1600818 100644 --- a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap +++ b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap @@ -181,7 +181,7 @@ Object { "errorCode": undefined, "errorReason": undefined, "innerError": undefined, - "message": "Maximum number of reconnect attempts reached!", + "message": "Maximum number of reconnect attempts reached! (5)", "name": "MaxAttemptsReachedOnReconnectingError", } `; diff --git a/packages/web3-errors/test/unit/errors.test.ts b/packages/web3-errors/test/unit/errors.test.ts index a267ad5dd7c..8492aaa3e00 100644 --- a/packages/web3-errors/test/unit/errors.test.ts +++ b/packages/web3-errors/test/unit/errors.test.ts @@ -127,7 +127,7 @@ describe('errors', () => { describe('MaxAttemptsReachedOnReconnectingError', () => { it('should have valid json structure', () => { expect( - new connectionErrors.MaxAttemptsReachedOnReconnectingError().toJSON(), + new connectionErrors.MaxAttemptsReachedOnReconnectingError(5).toJSON(), ).toMatchSnapshot(); }); }); diff --git a/packages/web3-providers-ipc/test/integration/reconnection.test.ts b/packages/web3-providers-ipc/test/integration/reconnection.test.ts index d140833c13d..fb9645a92b5 100644 --- a/packages/web3-providers-ipc/test/integration/reconnection.test.ts +++ b/packages/web3-providers-ipc/test/integration/reconnection.test.ts @@ -97,8 +97,8 @@ describeIf(isIpc)('IpcSocketProvider - reconnection', () => { web3Provider._socketConnection?.end(); const errorEvent = waitForEvent(web3Provider, 'error'); - const errorMessage = await errorEvent; - expect(errorMessage).toBe(`Max connection attempts exceeded (${3})`); + const error = (await errorEvent) as Error; + expect(error.message).toBe(`Maximum number of reconnect attempts reached! (${3})`); }); }); }); diff --git a/packages/web3-providers-ws/test/integration/reconnection.test.ts b/packages/web3-providers-ws/test/integration/reconnection.test.ts index f0b9e735903..bd406e62e58 100644 --- a/packages/web3-providers-ws/test/integration/reconnection.test.ts +++ b/packages/web3-providers-ws/test/integration/reconnection.test.ts @@ -124,16 +124,18 @@ describeIf(isWs && !isBrowser)('WebSocketProvider - reconnection', () => { // @ts-expect-error run protected method web3Provider._addSocketListeners(); const errorEvent = new Promise(resolve => { - web3Provider.on('error', message => { - if (typeof message === 'string') { - resolve(message); + web3Provider.on('error', error => { + if ( + error?.message?.startsWith('Maximum number of reconnect attempts reached') + ) { + resolve(error); } }); }); await server.close(); - const errorMessage = await errorEvent; - expect(errorMessage).toBe(`Max connection attempts exceeded (${3})`); + const error = (await errorEvent) as Error; + expect(error.message).toBe(`Maximum number of reconnect attempts reached! (${3})`); }); }); }); diff --git a/packages/web3-utils/src/socket_provider.ts b/packages/web3-utils/src/socket_provider.ts index 762a5d83ad8..1862166c148 100644 --- a/packages/web3-utils/src/socket_provider.ts +++ b/packages/web3-utils/src/socket_provider.ts @@ -37,6 +37,7 @@ import { ConnectionError, ConnectionNotOpenError, InvalidClientError, + MaxAttemptsReachedOnReconnectingError, PendingRequestsOnReconnectingError, RequestAlreadySentError, Web3WSProviderError, @@ -288,8 +289,10 @@ export abstract class SocketProvider< this.isReconnecting = false; this._clearQueues(); this._removeSocketListeners(); - const errorMsg = `Max connection attempts exceeded (${this._reconnectOptions.maxAttempts})`; - this._eventEmitter.emit('error', errorMsg); + this._eventEmitter.emit( + 'error', + new MaxAttemptsReachedOnReconnectingError(this._reconnectOptions.maxAttempts), + ); } }