Skip to content

Commit

Permalink
Use MaxAttemptsReachedOnReconnectingError similar to v1.x (#5894)
Browse files Browse the repository at this point in the history
* use `MaxAttemptsReachedOnReconnectingError` similar to v1.x

* update CHANGELOG.md files
  • Loading branch information
Muhammad-Altabba authored Mar 9, 2023
1 parent 36bd823 commit 7a9c2bb
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 28 deletions.
16 changes: 10 additions & 6 deletions docs/docs/guides/web3_migration_guide/providers_migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
}
});
Expand Down
22 changes: 13 additions & 9 deletions docs/docs/guides/web3_providers_guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
}
});
Expand Down Expand Up @@ -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`.
}
});
```
1 change: 1 addition & 0 deletions packages/web3-errors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/web3-errors/src/errors/connection_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
`;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-errors/test/unit/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
});
});
});
12 changes: 7 additions & 5 deletions packages/web3-providers-ws/test/integration/reconnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
});
});
});
7 changes: 5 additions & 2 deletions packages/web3-utils/src/socket_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
ConnectionError,
ConnectionNotOpenError,
InvalidClientError,
MaxAttemptsReachedOnReconnectingError,
PendingRequestsOnReconnectingError,
RequestAlreadySentError,
Web3WSProviderError,
Expand Down Expand Up @@ -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),
);
}
}

Expand Down

0 comments on commit 7a9c2bb

Please sign in to comment.