question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

networkError method should define a request attribute in mocked error response

See original GitHub issue

When calling networkError, the error returned by the lib should define a request attribute. This is how axios works. It’s important because error handling rely on the error.response check. Such as:

.catch((error) => {
        // Error
        if (error.response) {
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            // console.log(error.response.data);
            // console.log(error.response.status);
            // console.log(error.response.headers);
        } else if (error.request) {   //    <--------- THIS CHECK
            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            console.log(error.request);
        } else {
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
        }
        console.log(error.config);
    });

I’ve managed to workaround this by replying [0,‘Network Error’]. But, I guess it’s not the ideal solution.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

4reactions
tiagobnobregacommented, Apr 21, 2020

@dreyks What I do is normalize error object to always have a statusCode attribute as a root property. it first tries to take the status from the response, then checks for request and finally defaults to some code. This way, even if it has a response with a status of “0” (zero). That code will be defined. I currently have this (in typescript):

interface RequestError extends AxiosError {
  status: number;
  data?: object;
}

function normalizeRequestError(error: AxiosError): RequestError {
  const {response, request} = error;
  if (response) {
    /*
     * The request was made and the server responded with a
     * status code that falls out of the range of 2xx
     */
    const {data, status: responseStatus} = response;
    return {
      ...error,
      status: responseStatus, // <- This would be zero in the mock returning [0, 'Network Error']
      data,
    };
  } else if (request) {
    /*
     * The request was made but no response was received, `error.request`
     * is an instance of XMLHttpRequest in the browser and an instance
     * of http.ClientRequest in Node.js
     */
    return {
      ...error,
      status: 0, //<- also set to zero in actual code
    };
  } else {
    //* Something happened in setting up the request and triggered an Error
    return {
      ...error,
      status: -1,
    };
  }
}
0reactions
PhakornKiongcommented, May 5, 2021

Would like to know if there is any progress on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest returns "Network Error" when doing an authenticated ...
I'm trying to test an actual (ie. real network) request with Jest. These are the tested scenarios: Test an external API (fixer.io) with...
Read more >
networkError() - Api - Mock Service Worker Docs
networkError(). Emulates a network error during the response. Cancels the respective request. Intended vs unintended exceptions.
Read more >
Error handling - Apollo GraphQL Docs
A client sent the hash of a query string to execute via automatic persisted queries, but the server has disabled APQ. OPERATION_RESOLUTION_FAILURE. The...
Read more >
axios-mock-adapter - npm
Axios adapter that allows to easily mock requests. Latest version: 1.21.2, ... Returns a failed promise with Error('Network Error'); mock.
Read more >
Debugging Apollo GraphQL MockedProvider - Swarmia
This assertion would be useful to know if a test case ended up doing ... [Network error]: Error: No more mocked responses for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found