facebook/react-native

fetch needs patched for try/catch on JSON.parse

niftylettuce opened this issue · 6 comments

Please fix

return this.text().then(JSON.parse)
to have try/catch surrounding JSON.parse?

also why are you throwing errors instead of sending them back with Promise.reject?

return Promise.reject(new TypeError('Already read'))
uses reject but this doesn't
throw new Error('could not read FormData body as blob')

Basically because you are throwing errors and not wrapping JSON.parse in a try/catch block - I had to dig deep to find out that my 404 response could not use res.json() in this example:

        fetch(apiUri + path, {
          method: 'GET',
          headers: that.headers
        })
        .then((res) => res.json())
        .then((res) => {
          if (res && res.error)
            return fn(res.error);
          fn(null, res);
        })
        .catch(fn)

The error is thrown that it can't parse JSON (the body is text response)

The reason I have to use fetch polyfill is because superagent doesn't work (because it's trying to run in a node runtime environment and not a browser environment or something). This is broken and we can't even hook in a simple API into react-native...

if an error is thrown inside the .then callback, isn't it supposed to be propagated to the .catch callback?

Hey niftylettuce, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If this is a feature request or a bug that you would like to be fixed by the team, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • If you don't know how to do something or not sure whether some behavior is expected or a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • We welcome clear issues and PRs that are ready for in-depth discussion; thank you for your contributions!

Closing as per discussion in JakeChampion/fetch#235