Unable to catch error thrown in WebSocketProvider #2896
Replies: 4 comments 1 reply
-
You can't catch the exception, the only thing that you can do, is listen to
|
Beta Was this translation helpful? Give feedback.
-
As stated in #2921, this problem occurs (potentially among other reasons) when the RPC provider returns code 503 or 504. |
Beta Was this translation helpful? Give feedback.
-
I encountered this problem again today. As a workaround, I extracted the initialization of the provider into a method that resolves only if the websocket of the This is probably not a perfect solution, but maybe it is helpful for somebody else too: const getInitializedWebsocketProvider = (rpcUrl: string) => {
// listen for errors during handshake and wait until websocket is opened to prevent uncatched errors:
// https://github.com/ethers-io/ethers.js/discussions/2896
return new Promise<ethers.providers.WebSocketProvider>((resolve, reject) => {
const provider = new ethers.providers.WebSocketProvider(rpcUrl);
(provider.websocket as unknown as EventEmitter)
.once('open', () => {
resolve(provider);
})
.once('error', (error) => {
reject(error);
});
});
}; |
Beta Was this translation helpful? Give feedback.
-
Hi there, I'm having a similar problem with the More practically, in my context, I'm instantiating a provider, a wallet, and sending a transaction, all within a single http request to my web server. This is because my web server is agnostic as to which network the user wants to send a transaction to. In that case, I'm unable to wait for a potential Shouldn't the provider also emit an event when instantiated with success? (similarly to the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey,
I am trying to use the
WebSocketProvider
of ethers. Unfortunately, the websocket RPC of the Fantom Opera network is wonky at the moment and causes errors in my application. I would like to catch these errors and fallback fallback to another RPC if an error with theWebSocketProvider
occurs. Unfortunately, I am not able to catch the error that is thrown when using the provider. Instead of being able to catch and handle the error, my whole application crashes.As far as i see, the behaviour is not specific to the Fantom Opera network, but happens in other error cases too. For example, I am not able to catch the error that happens when specifying an invalid websocket URL:
Instead of printing
next command
and exiting gracefully, the code snippet above exits with an error code and leads to the following output:I am not sure if this is something that should be solved inside of ethers or if there are other preferred solution. But I would be thankful for any hint how to handle such errors gracefully 🙂
Thanks a lot in advance!
Beta Was this translation helpful? Give feedback.
All reactions