elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

404 not caught by .notFound on Android (React Native) #92

Closed fritzfr closed 4 years ago

fritzfr commented 4 years ago

Hey,

I'm encountering a consistency issue on React Native, I'm not sure whether it's an issue of wretch or it's a React Native issue.

Consider the following method:

    async isUsernameAvailable(username) {
        try {
            return await api
                .url(`/users/${username}`)
                .head()
                /* Wrongly formatted, return false */
                /* This is not being called on Android */
                .badRequest(() => false)
                /* Return true when not found */
                /* This is not being called on Android */
                .notFound(() => true)
                .res((r) => {
                    if (r.status === 200) {
                        // 200 means username taken
                        // thus return false
                        return false;
                    }

                    return true;
                });
        } catch (e) {
            /* Android goes here if the status is >= 400 */
            return false;
        }
    },

I'm using a HEAD request in this method, which returns then true or false based on the response code. On Android, it always returns false, so I discovered that the notFound or the badRequest handlers are not being called. The catch block catches:

[TypeError: Network request failed]

instead of the handlers being called. The server is a remote server (not localhost) and does use SSL (https). I can see the requests coming in on the server and returning 404. On iOS, 404 gets caught by notFound as expected.

Any idea why this might be or if the issue is not with this module? I guess it could also be an implementation issue of fetch within react native android.

elbywan commented 4 years ago

Hi @fritzfr!

Any idea why this might be or if the issue is not with this module? I guess it could also be an implementation issue of fetch within react native android.

Well I have a very limited knowledge of react-native, but I searched for a bit and I think that (indeed) the issue comes from the fetch implementation: https://github.com/facebook/react-native/issues/30055

fritzfr commented 4 years ago

@elbywan Yes, I found the exact same issue just a few seconds ago. Thanks for your help!