Skip to content Skip to sidebar Skip to footer

Why Do Fetch Errors Not Have A Stacktrace In My Single Page Application?

I have two simple wrappers that handle requests in my single page application. One wraps fetch and throws an error if a response is not ok (not in the 200-300 range): const fetchy

Solution 1:

The fetch error is created asynchronously & not directly related to a particular line of JavaScript. Although I agree it would be helpful if the line of the fetch call was included. I've filed a bug for this https://bugs.chromium.org/p/chromium/issues/detail?id=718760

As a workaround, you could catch the fetch error, and throw a new error if there are no numbers in the stack:

functionfetchy(...args) {
  returnfetch(...args).catch(err => {
    if (!err.stack.match(/\d/)) throwTypeError(err.message);
    throw err;
  }).then(response => {
    if (response.ok) return response;
    throwError(response.statusText);
  });
}

Here's an example of that running http://jsbin.com/qijabi/edit?js,console

Solution 2:

Recently I faced the same error. Production channel logged this error for about 500 times in just 2 months and it really got irritating. Ours’ is a rails application with frontend powered by react.

Here is what was happening in our case. When the page loads, the refresh button changes to cross button, now if some api request is in progress during this page loading time and the user click this cross button, then chrome browser throws this error. For the same situation Firefox throws NetworkError when attempting to fetch resource.This is not really an issue which we should be worried about, and so we decided to make sentry ignore this error by using the ignoreErrors attribute of sentry.

Sentry.init({
  dsn: "sentry_dsn",
  ignoreErrors: [
    'TypeError: Failed to fetch',
    'TypeError: NetworkError when attempting to fetch resource.'
  ],
});

Note: Failed to fetch is also generated by CORS errors, please be mindful of that too. Also we decided to ignore errors with statusCode in between 400 to 426 using the beforeSend callback of sentry.

I spent days trying to find this error. Hope this helps somebody.

Originally I wrote this response on this page - https://forum.sentry.io/t/typeerror-failed-to-fetch-reported-over-and-overe/8447/2

Thank you

Post a Comment for "Why Do Fetch Errors Not Have A Stacktrace In My Single Page Application?"