61

I'm logging JS client errors using Sentry and there's a lot of TypeError: cancelled errors. It's only occurring on iOS. I can't find anything on Google. Is this a native Javascript error or something else? What does it mean?

enter image description here

I also get similar errors in other languages, such as 취소됨, Abgebrochen, and cancelado. This tells me that the error isn't raised by my code.

7
  • I've seen the same in my Sentry setup and it's limited to iOS 12.2 and mobile Safari 12.1 so far. No stack trace included, so it's very cryptic. I'm not sure what the issue is and haven't seen anything come up in related search results. "Cancelled" portion of error message leads me to believe this could possibly be related to either promises or fetching.
    – mcabrams
    May 2, 2019 at 22:09
  • Are you maybe sending a CORS request to a site that doesn't allow CORS?
    – Aplet123
    May 2, 2019 at 22:49
  • @Aplet123 it works fine for ~97% of users, including iOS users. Maybe it has something to do with jailbroken phones with adblockers, etc.
    – Leo Jiang
    May 2, 2019 at 22:57
  • 4
    We faced the same issues in Sentry logs. There is an CORS issue starting from IOS 11. We solved an issue this way stackoverflow.com/a/52004250 May 6, 2019 at 12:53
  • @AntonAntonov Thanks for the link, CORS issues is a strong possibility, but my requests don't have any headers
    – Leo Jiang
    May 8, 2019 at 16:03

5 Answers 5

33

Frustrating right?

Recently our team encountered this same error. 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 iOS chrome/safari throws this error. For the same situation Firefox browser throws TypeError: NetworkError when attempting to fetch resource and chrome browser throughs TypeError: Failed to fetch.

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.',
    'TypeError: cancelled'
  ],
});

**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.

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

Thank you

Also, This was originally written here: https://forum.sentry.io/t/typeerror-failed-to-fetch-reported-over-and-overe/8447/2

5
  • 1
    Should cancelled be lowercase? Because this isn't working for me currently. May 25, 2021 at 18:42
  • 1
    yes @FernandoRojo it should be lowercased Aug 15, 2021 at 16:45
  • See also similar errors in stackoverflow.com/questions/49343024/… Nov 19, 2021 at 20:16
  • Wow, this is really helpful - also been scratching my head on this for days.
    – Android
    Apr 19, 2022 at 17:51
  • Noticed this error as I'm working on filtering out non-actionable errors in Sentry now as well. One interesting thing I'm seeing is that it seems that we get the same error in different locales/translations. For example, I see cancelled as the exception value, but I also see exception values for 'cancelado', 'anulowane', 'annulé', 'đã hủy', 'отменено' which all translate back to the string 'canceled` in English. There is not much additional detail in Sentry to filter out this error.
    – hybrid
    Nov 7 at 16:00
18

According to the WHATWG standard: https://html.spec.whatwg.org/multipage/browsing-the-web.html#aborting-a-document-load, fetch requests will be cancelled when aborting the document (close or navigate away). Apparently browsers are behaving differently so I made a tool to test the browser behavior if that helps: https://request-cancellation-test.vercel.app (code).

Here're the test results for common browsers:

Field Chrome 95 Safari 14 iOS Safari 14 Firefox 94
.toString() TypeError: Failed to fetch TypeError: cancelled TypeError: cancelled TypeError: NetworkError when attempting to fetch resource.
.name TypeError TypeError TypeError TypeError
.message Failed to fetch cancelled cancelled NetworkError when attempting to fetch resource.
.stack TypeError: Failed to fetch at ...
6

If you are using the fetch API, it could be a problem with AbortController and AbortSignal in iOS 11.1-12, which would only be firing when someone tries to abort a fetch request (which is why it wouldn't necessarily affect all iOS users, explaining the inconsistency).

To elaborate, iOS 11.1-12 defines AbortController and AbortSignal in the DOM, but they are a stub - see here. So if you try to abort a fetch request in iOS in <= 12, the request will not abort and would likely throw some sort of error.

Given it is a TypeError rather than an AbortError it would seem likely that the issue is with the AbortController not being properly/fully defined.

EDIT: Further reading also seems to indicate that failed fetches in iOS throw TypeError errors, even for things like blocked fetches. As mentioned above, the issue could be with any ad-blockers installed (say, on a jailbroken iPhone) or a CORS issue, and iOS would then throw TypeError - Webkit BugZilla discussion. As such, concentrating on the error type might lead you down the wrong path.

1

Here is another possibility for idiots like myself who did this ...

<form action="blah">
   <button onClick="javascript:submitForm();">Do It</button>
</form>
function submitForm() {

  fetch('https://api.mailerlite.com/api/v2/groups/group_name/subscribers', options)
       .then(response => response.json())
       .then(response => console.log(response))
       .catch(err => console.error(err));
   }

}

This will give the dreaded TypeError: cancelled

It's because its breaking off the connection and trying to do the post of the actual form you defined (which wasn't even going anywhere)

SOLUTION:

<form action="blah">
   <button onClick="javascript:submitForm(event);">Do It</button>
</form>
function submitForm(event) {

  event.preventDefault()

  fetch('https://api.mailerlite.com/api/v2/groups/group_name/subscribers', options)
       .then(response => response.json())
       .then(response => console.log(response))
       .catch(err => console.error(err));
   }

}

This passes the event thru to the function where you can cancel the Form submission (using preventDefault) and then get on with your fetch.

Very simple stuff, but very easy to forget and have you pulling your hair out, especially coupled with that Fantastically Descriptive and Useful error message.

0

I had to email Sentry to get the error right here.

I turns out, I was sending errors in the extra field, which isn't accounted for by the ignoreErrors property. I solved it like so:

const ignoreErrors = [
      'TypeError: NetworkError when attempting to fetch resource.',
      'TypeError: cancelled',
      'TypeError: Cancelled',
      'message: cancelled',
      'cancelled',
]

Sentry.init({
    async beforeSend(event) {
      // this is using typescript, but change it as you need
      const message = (event?.extra as
        | undefined
        | {
            error?: { message?: string }
          })?.error?.message?.toLowerCase()

      if (message && ignoreErrors.includes(message)) {
        return null // don't send
      }
      return event
    },
    ignoreErrors,
})
2
  • Do you know, what causes 'message: cancelled' and 'cancelled'? Are you confident that they can be ignored? Nov 15, 2021 at 22:02
  • 1
    It's been a while, so I wish I had a better answer. But I think it's just due to network issues and can be ignored. I would do some research on it though. Nov 16, 2021 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.