lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.66k stars 102 forks source link

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

Closed TimDaub closed 1 year ago

TimDaub commented 2 years ago

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',
  }
rukshn commented 2 years ago

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.

lquixada commented 1 year ago

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