lquixada / cross-fetch

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

res.json() Error type instance #170

Closed mharj closed 11 months ago

mharj commented 11 months ago

According fetch documentation and also quickly tested how browser works, I think res.json() => should throw SyntaxError if payload is not valid JSON one .. currently I'm getting FetchError instance instead on unit testing.

AssertionError: expected FetchError{ …(3) } to be an instance of SyntaxError

Example from Browser console (when in https://www.google.com)

> const res = await fetch("https://www.google.com");
< undefined
> await res.json();
< Uncaught SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
lquixada commented 11 months ago

@mharj thanks for reporting. cross-fetch is just a proxy lib that decides which implementation to use based on the environment it's running on. If it's Node.js, it will delegate to node-fetch. If it's in a browser, whatwg-fetch is the chosen lib. Please reach out to one of these repos to report implementation issues.