Skip to content

uncaughtException when instantiating JsonRpcProvider and adding subscription in bad network conditions #3924

Open
@deodad

Description

@deodad

Ethers Version

6.2.0

Search Terms

No response

Describe the Problem

When instantiating a JsonRpcProvider and setting up a listener, an thrown exception is thrown if the rpc url returns a bad status code and there doesn't seem to be any way to catch it.

https://replit.com/@deodad/Ethers-Uncaught-Exception?v=1

Code Snippet

import { JsonRpcProvider } from "ethers";
import nock from 'nock';

process.on('uncaughtException', (err) => {
  console.error("but instead it is caught here, uncaught exception:", err)
  process.exit(1);
})

nock.back.setMode('lockdown');
nock('https://eth-goerli.g.alchemy.com').post('/v2/apikey').reply(500);

/** 
 * How do we instantiate a JsonRpcProvider and attach event listeners 
 * in a way that we avoid any uncaughtExceptions?
*/
(async () => {
  try {
    const provider = new JsonRpcProvider('https://eth-goerli.g.alchemy.com/v2/apikey');
    await provider.on('block', console.log)
  } catch (e) {
    console.log("we want to deal with the error here")
  }
})()

Contract ABI

No response

Errors

No response

Environment

node.js (v12 or newer)

Environment (Other)

No response

Metadata

Metadata

Assignees

Labels

bugVerified to be an issue.fixed/completeThis Bug is fixed or Enhancement is complete and published.v6Issues regarding v6

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions