Skip to content

Add Error when rpc url fails. #1238

Open
@ikemHood

Description

@ikemHood

Is your feature request related to a problem? Please describe.

I've been using alchemy rpc for a while and it works fine wothout appending an apikeys sometimes.

maybe a change from them or something but it suddenly didn't work anymore with 404 error..

making contract calls now fails with

2415 |    * @param response string[] - response from the method
2416 |    * @return Result - parsed response corresponding to the abi
2417 |    */
2418 |   parse(method, response) {
2419 |     const { outputs } = this.abi.find((abi) => abi.name === method);
2420 |     const responseIterator = response.flat()[Symbol.iterator]();
                                    ^
TypeError: undefined is not an object (evaluating 'response.flat')
      at parse (/home/ikem/dev/horuslab/karst-backend/node_modules/starknet/dist/index.mjs:2420:30)

Throwing a better error when the apicall fails make it easier to debug and find the actual cause of a misbehaior

A little digging and await rawResult.json(); here returns error as undefind when this error happens

  async fetchEndpoint(method, params) {
    try {
      const rawResult = await this.fetch(method, params, this.requestId += 1);
      const { error, result } = await rawResult.json();
      console.log({error, result})
      this.errorHandler(method, params, error);
      return result;
    } catch (error) {
      this.errorHandler(method, params, error?.response?.data, error);
      throw error;
    }
  }

{ error: undefined, result: undefined, }

Describe the solution you'd like
Throw a detail rpc error when the api call fails

  async fetchEndpoint(method, params) {
    try {
      const rawResult = await this.fetch(method, params, this.requestId += 1);
      const { error, result } = await rawResult.json();
      if(!error && !result){
        throw this.errorHandler(method, params, error);
      }
      this.errorHandler(method, params, error);
      return result;
    } catch (error) {
      this.errorHandler(method, params, error?.response?.data, error);
      throw error;
    }
  }

Metadata

Metadata

Assignees

Labels

Type: bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions