MoralisWeb3 / Moralis-JS-SDK

Moralis Official Javascript SDK
https://docs.moralis.io
Other
368 stars 257 forks source link

Cannot destructure property ‘status’ of ‘error.response’ as it is undefined #448

Closed shreetheja closed 2 years ago

shreetheja commented 2 years ago

New Bug Report

Checklist

Issue Description

Whenever a request is made via moralis js sdk it boils down to a fetchApi Fucntion in the route node_modules\moralis\lib\node\MoralisWeb3Api.js:189:9. And this FetchApi function makes a request through axios package see the code below

note: this code is not written by me its inside moralis package thats mentioned above route.

static async fetchFromApi(endpoint, params) {
    const {
      method = 'GET',
      url,
      bodyParams
    } = endpoint;

    try {
      const parameterizedUrl = this.getParameterizedUrl(url, params);
      const body = this.getBody(params, bodyParams);
      const response = await axios(this.baseURL + parameterizedUrl, {
        params,
        method,
        body,
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
          'x-api-key': this.apiKey
        }
      });
      const result = response.data;
      const nextOptions = this.getNextOptions(result, params);
      if (!this.checkObjEqual(nextOptions, params)) result.next = () => this.fetchFromApi(endpoint, nextOptions);
      return result;
    } catch (error) {
      const {
        status,
        headers,
        data
      } = error.response;
      let msg;

      if (status === 429) {
        msg = `This Moralis Server is rate-limited because of the plan restrictions. See the details about the current rate and throttle limits: ${JSON.stringify(this.getApiRateLimitInfo(headers))}`;
      } else {
        msg = this.getApiErrorMessage(error, url);
      }

      throw new Error(msg);
    }
  }

And while making the request if the server sends non 200 status then axios will throw a error this error is caught by a catch block. Inside Catch block first step is to destructure the error variable to const { status, headers, data } = error.response When this happens the status is undefined and another error is thrown back to the main code.

Steps + code to reproduce

async getOwnerOfContract(contractAddress, abi) {
     await Moralis.start({ moralisSecret });
    try {
      const options = {
        chain: network,
        address: contractAddress,
        function_name: 'owner',
        abi,
        params: {},
      };
      const owner = await Moralis.Web3API.native.runContractFunction(options);
      console.log(owner);
      return owner;
    } catch (error) {
      logger.error('Error at Moralis.getOwnerOfContract', error);
      throw error;
    }
  }

Run this above function with necessary variables but make sure to supply a wrong parameter abi to intentionally get a wrong response.

Actual Outcome

Actual Outcome received is a Destructure error as explained above.

[error] TypeError: Cannot destructure property 'status' of 'error.response' as it is undefined.
            at Function.fetchFromApi (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:207:9) 
            at Function.fetch (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:178:17)       
            at Object.runContractFunction (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:334:56)
            at MoralisHandler._callee23$ (C:/Users/snsha/Documents/event-api/utils/moralis.js:427:50)
            at tryCatch (C:\Users\snsha\Documents\event-api\node_modules\regenerator-runtime\runtime.js:65:40)
            at Generator.invoke [as _invoke] (C:\Users\snsha\Documents\event-api\node_modules\regenerator-runtime\runtime.js:303:22)
            at Generator.prototype.<computed> [as next] (C:\Users\snsha\Documents\event-api\node_modules\regenerator-runtime\runtime.js:117:21)
            at step (C:\Users\snsha\Documents\event-api\utils\moralis.js:26:191)
            at C:\Users\snsha\Documents\event-api\utils\moralis.js:26:437
            at new Promise (<anonymous>)
            at MoralisHandler.<anonymous> (C:\Users\snsha\Documents\event-api\utils\moralis.js:26:99)
            at MoralisHandler.getOwnerOfContract (C:\Users\snsha\Documents\event-api\utils\moralis.js:1060:23)
            at Timeout._onTimeout (C:/Users/snsha/Documents/event-api/app.js:29:24)
            at listOnTimeout (internal/timers.js:555:17)
            at processTimers (internal/timers.js:498:7)

Expected Outcome

Expected outcome is a error saying parameter abi is incorrect.

Error: param abi is required!
    at C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:83:29
    at Array.forEach (<anonymous>)
    at Function.getBody (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:77:16)
    at Function.fetchFromApi (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:174:25)        
    at Function.fetch (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:162:17)
    at Object.runContractFunction (C:\Users\snsha\Documents\event-api\node_modules\moralis\lib\node\MoralisWeb3Api.js:309:56)   
    at MoralisHandler._callee23$ (C:/Users/snsha/Documents/event-api/utils/moralis.js:427:50)
    at tryCatch (C:\Users\snsha\Documents\event-api\node_modules\regenerator-runtime\runtime.js:65:40)
    at Generator.invoke [as _invoke] (C:\Users\snsha\Documents\event-api\node_modules\regenerator-runtime\runtime.js:303:22)    
    at Generator.prototype.<computed> [as next] (C:\Users\snsha\Documents\event-api\node_modules\regenerator-runtime\runtime.js:117:21)
    at step (C:\Users\snsha\Documents\event-api\utils\moralis.js:26:191)
    at C:\Users\snsha\Documents\event-api\utils\moralis.js:26:437
    at new Promise (<anonymous>)
    at MoralisHandler.<anonymous> (C:\Users\snsha\Documents\event-api\utils\moralis.js:26:99)
    at MoralisHandler.getOwnerOfContract (C:\Users\snsha\Documents\event-api\utils\moralis.js:1060:23)
    at Timeout._onTimeout (C:/Users/snsha/Documents/event-api/app.js:29:24)
    at listOnTimeout (internal/timers.js:555:17)
    at processTimers (internal/timers.js:498:7)

Environment

Server

Client

Logs

N/A

ErnoW commented 2 years ago

This was related to a bug in the way we used axios. This has been fixed in v1.10