Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MaxAttemptsReachedOnReconnectingError similar to v1.x #5894

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})`);
});
});
});
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/web3/web3.js/runs/11848321915 codecov seems to be failing, otherwise PR LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @luu-alex ,
Actually, this is tested twice inside web3-providers-ws and web3-providers-ipc. But CodeCov does not check across packages. So, I think it is safe to ignore it. Or what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we have a dedicated test in web3-utils for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we can. But it will require a complex scenario that is already tested twice with IpcProvider and WebSocketProvider. So, writing the test will be just time-consuming, I think 😊 .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is tested twice inside web3-providers-ws and web3-providers-ipc. But CodeCov does not check across packages

currently codecov is only checking unit-testing inside packages and not counting any coverage for tests across packages ( integration tests )

new MaxAttemptsReachedOnReconnectingError(this._reconnectOptions.maxAttempts),
);
}
}

Expand Down