Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 7a9c2bb

Browse files
Use MaxAttemptsReachedOnReconnectingError similar to v1.x (#5894)
* use `MaxAttemptsReachedOnReconnectingError` similar to v1.x * update CHANGELOG.md files
1 parent 36bd823 commit 7a9c2bb

File tree

9 files changed

+42
-28
lines changed

9 files changed

+42
-28
lines changed

docs/docs/guides/web3_migration_guide/providers_migration_guide.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,25 @@ const reconnectOptions: ReconnectOptions = {
165165
};
166166
```
167167

168-
##### Error message for reconnect attempts
168+
#### Error message for reconnect attempts
169+
170+
:::note
171+
This section applies for both `IpcProvider` and `WebSocketProvider`.
172+
:::
169173

170174
The error in, version 1.x, was an Error object that contains the message:
171175
`'Maximum number of reconnect attempts reached!'`
172176

173-
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:
177+
And, the error in version 4.x, is the same, but will also contain the value of the variable `maxAttempts` as follows:
174178

175-
`` `Max connection attempts exceeded (${maxAttempts})` ``
179+
`` `Maximum number of reconnect attempts reached! (${maxAttempts})` ``
176180

177181
And here is how to catch the error, in version 4.x, if max attempts reached when there is auto reconnecting:
178182

179183
```ts
180-
provider.on('error', errorMessage => {
181-
if (errorMessage.startsWith('Max connection attempts exceeded')) {
182-
// the `errorMessage` will be `Max connection attempts exceeded (${maxAttempts})`
184+
provider.on('error', error => {
185+
if (error.message.startsWith('Maximum number of reconnect attempts reached!')) {
186+
// the `error.message` will be `Maximum number of reconnect attempts reached! (${maxAttempts})`
183187
// the `maxAttempts` is equal to the provided value by the user, or the default value `5`.
184188
}
185189
});

docs/docs/guides/web3_providers_guide/index.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ Here is how to catch the error if max attempts reached when the auto reconnectin
159159

160160
```ts
161161
provider.on('error', errorMessage => {
162-
if (errorMessage.startsWith('Max connection attempts exceeded')) {
163-
// the `errorMessage` will be `Max connection attempts exceeded (${maxAttempts})`
162+
if (errorMessage.startsWith('Maximum number of reconnect attempts reached!')) {
163+
// the `errorMessage` will be `Maximum number of reconnect attempts reached! (${maxAttempts})`
164164
// the `maxAttempts` is equal to the provided value by the user or the default `5`.
165165
}
166166
});
@@ -188,19 +188,23 @@ const reconnectOptions: ReconnectOptions = {
188188
};
189189
```
190190

191-
##### Error message for reconnect attempts
191+
#### Error message for reconnect attempts
192192

193-
The error message (not wrapped in an Error object) for the max reconnect attempts, will contain the value of the variable `maxAttempts` as follows:
193+
:::note
194+
This section applies for both `IpcProvider` and `WebSocketProvider`.
195+
:::
196+
197+
The error message, for the max reconnect attempts, will contain the value of the variable `maxAttempts` as follows:
194198

195-
`` `Max connection attempts exceeded (${maxAttempts})` ``
199+
`` `Maximum number of reconnect attempts reached! (${maxAttempts})` ``
196200

197201
And here is how to catch the error, if max attempts reached when there is auto reconnecting:
198202

199203
```ts
200-
provider.on('error', errorMessage => {
201-
if (errorMessage.startsWith('Max connection attempts exceeded')) {
202-
// the `errorMessage` will be `Max connection attempts exceeded (${maxAttempts})`
203-
// the `maxAttempts` is equal to the provided value by the user or the default `5`.
204+
provider.on('error', error => {
205+
if (error.message.startsWith('Maximum number of reconnect attempts reached!')) {
206+
// the `error.message` will be `Maximum number of reconnect attempts reached! (${maxAttempts})`
207+
// the `maxAttempts` is equal to the provided value by the user, or the default value `5`.
204208
}
205209
});
206210
```

packages/web3-errors/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7676
### Changed
7777

7878
- The abstract class `Web3Error` is renamed to `BaseWeb3Error` (#5771)
79+
- Using `MaxAttemptsReachedOnReconnectingError` with the same message for 1.x but also adding the `maxAttempts` (#5894)
7980

8081
### Added
8182

packages/web3-errors/src/errors/connection_errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export class ConnectionCloseError extends ConnectionError {
9191
}
9292

9393
export class MaxAttemptsReachedOnReconnectingError extends ConnectionError {
94-
public constructor() {
95-
super('Maximum number of reconnect attempts reached!');
94+
public constructor(numberOfAttempts: number) {
95+
super(`Maximum number of reconnect attempts reached! (${numberOfAttempts})`);
9696
this.code = ERR_CONN_MAX_ATTEMPTS;
9797
}
9898
}

packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Object {
181181
"errorCode": undefined,
182182
"errorReason": undefined,
183183
"innerError": undefined,
184-
"message": "Maximum number of reconnect attempts reached!",
184+
"message": "Maximum number of reconnect attempts reached! (5)",
185185
"name": "MaxAttemptsReachedOnReconnectingError",
186186
}
187187
`;

packages/web3-errors/test/unit/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('errors', () => {
127127
describe('MaxAttemptsReachedOnReconnectingError', () => {
128128
it('should have valid json structure', () => {
129129
expect(
130-
new connectionErrors.MaxAttemptsReachedOnReconnectingError().toJSON(),
130+
new connectionErrors.MaxAttemptsReachedOnReconnectingError(5).toJSON(),
131131
).toMatchSnapshot();
132132
});
133133
});

packages/web3-providers-ipc/test/integration/reconnection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ describeIf(isIpc)('IpcSocketProvider - reconnection', () => {
9797
web3Provider._socketConnection?.end();
9898
const errorEvent = waitForEvent(web3Provider, 'error');
9999

100-
const errorMessage = await errorEvent;
101-
expect(errorMessage).toBe(`Max connection attempts exceeded (${3})`);
100+
const error = (await errorEvent) as Error;
101+
expect(error.message).toBe(`Maximum number of reconnect attempts reached! (${3})`);
102102
});
103103
});
104104
});

packages/web3-providers-ws/test/integration/reconnection.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,18 @@ describeIf(isWs && !isBrowser)('WebSocketProvider - reconnection', () => {
124124
// @ts-expect-error run protected method
125125
web3Provider._addSocketListeners();
126126
const errorEvent = new Promise(resolve => {
127-
web3Provider.on('error', message => {
128-
if (typeof message === 'string') {
129-
resolve(message);
127+
web3Provider.on('error', error => {
128+
if (
129+
error?.message?.startsWith('Maximum number of reconnect attempts reached')
130+
) {
131+
resolve(error);
130132
}
131133
});
132134
});
133135

134136
await server.close();
135-
const errorMessage = await errorEvent;
136-
expect(errorMessage).toBe(`Max connection attempts exceeded (${3})`);
137+
const error = (await errorEvent) as Error;
138+
expect(error.message).toBe(`Maximum number of reconnect attempts reached! (${3})`);
137139
});
138140
});
139141
});

packages/web3-utils/src/socket_provider.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
ConnectionError,
3838
ConnectionNotOpenError,
3939
InvalidClientError,
40+
MaxAttemptsReachedOnReconnectingError,
4041
PendingRequestsOnReconnectingError,
4142
RequestAlreadySentError,
4243
Web3WSProviderError,
@@ -288,8 +289,10 @@ export abstract class SocketProvider<
288289
this.isReconnecting = false;
289290
this._clearQueues();
290291
this._removeSocketListeners();
291-
const errorMsg = `Max connection attempts exceeded (${this._reconnectOptions.maxAttempts})`;
292-
this._eventEmitter.emit('error', errorMsg);
292+
this._eventEmitter.emit(
293+
'error',
294+
new MaxAttemptsReachedOnReconnectingError(this._reconnectOptions.maxAttempts),
295+
);
293296
}
294297
}
295298

0 commit comments

Comments
 (0)