cross-fetch icon indicating copy to clipboard operation
cross-fetch copied to clipboard

cross-fetch throws a "FetchError" but I can't find where "FetchError" is defined

Open TimDaub opened this issue 2 years ago • 1 comments

E.g. if I wanted to compare with instanceof where would I find the "FetchError" that cross-fetch throws when it comes across a non-parsable JSON body and throws upon await result.json().

I've not found anything on Google and MDN related to FetchError so I'm wondering where it stems from. Hopefully not my own code lol.

  FetchError {
    message: 'invalid json response body at https://test4.com/ reason: Unexpected token < in JSON at position 0',
    type: 'invalid-json',
  }

TimDaub avatar Jun 08 '22 14:06 TimDaub

I think this is not an error in cross-fetch.

When you make a get request to the test4.com website, which is likely to be a real website it will be sending an HTML document instead of a JSON payload, hence the first character of the returned html page is <

This makes the response an invalid JSON response, and that's is triggering this error. I suggest you to fetch content from a website/API endpoint that returns a JSON payload instead of HTML and see if that resolves this error.

rukshn avatar Jun 10 '22 09:06 rukshn

@TimDaub thanks for taking the time to open this issue. I assume that error is being thrown when running on Node since node-fetch is the lib that implements FetchError. Note that whatwg-fetch does not seem to implement it, so it's unlikely the instanceof comparison you've mentioned to work in browser environments.

lquixada avatar May 30 '23 14:05 lquixada