Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

How to catch a 404 when fetching an image, current .catch example does not work? #267

Closed
lakhman opened this issue Feb 15, 2017 · 5 comments

Comments

@lakhman
Copy link

lakhman commented Feb 15, 2017

Hi,

I make a fetch for a image like so:

RNFetchBlob.config({
    fileCache: false,
    appendExt: 'jpg', // Temp files will have a file extension
    key: image.fileName.split('/').pop()
}).fetch('GET', image.fileName).then((res) => {
    let uri = Platform.OS === 'android' ? 'file://' + res.path() : '' + res.path();
    // resolve(uri); // I use this in a promise
}).catch((errorMessage, statusCode) => {
    // This is never hit for a server 404 error
    console.log(errorMessage, statusCode);
});

The server i'm fetching from returns the correct headers and status code (from chrome:)

HTTP/1.1 404 Not Found
Date: Wed, 15 Feb 2017 17:35:24 GMT
Server: Apache/2.2.15 (CentOS)
Content-Length: 337
Keep-Alive: timeout=7, max=300
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

Instead, it saves the returned 404 HTML content into a file (fileCache: true).
/Users/user/Library/Developer/CoreSimulator/Devices/.../data/Containers/Data/Application/.../Documents/RNFetchBlob_tmp/

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
    <title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /uploads/img/201605/f123.jpg was not found on this server.</p>
<hr>
    <address>Apache/2.2.15 (CentOS) Server at site.com Port 443</address>
</body></html>

Am I doing something wrong? or is this a bug?

I should add:

I'm on iOS - Simulator - 10.2
Fetch Blob Version: ^0.10.2

@wkh237
Copy link
Owner

wkh237 commented Feb 16, 2017

@lakhman , you can find the status code via res.info(), it's a RNFetchBlobResponseInfo object.

@vu0tran
Copy link

vu0tran commented Feb 17, 2017

Correct me if I'm wrong, but I think what @lakhman means is that the .catch() block isn't firing.

I'm running into the same problem. I have a call that's returning a 401. I'm able to validate it through a curl call. However, RNFetchBlob always executes the .then() block and never jumps to the catch() even though it's not a 200 status.

   RNFetchBlob
      .config({
        path : RNFetchBlob.fs.dirs.DocumentDir + '/departments.json',
        fileCache : true,
      })
      .fetch('GET', 'https://..', {
      })
      .then((res) => {
        // Fires even though it's a 401
        alert(res.respInfo.status)
        console.log('The file saved to ', res.path())
        console.log(res)
      })
      .catch((error, statusCode) => {
        // Never fires
        alert('')
      })

@wkh237
Copy link
Owner

wkh237 commented Feb 17, 2017

The library will not throw an error because it's a successful request, just like how fetch treats the 4xx response. Catch block will only execute when network error happens.

see JakeChampion/fetch#155

@lakhman
Copy link
Author

lakhman commented Feb 20, 2017

@wkh237 that worked, perhaps the docs could be updated to provide a clearer example, I think catching 404 missing images would be a common use case and would help others!

+1 Thanks

@lakhman lakhman closed this as completed Feb 20, 2017
@piuccio
Copy link

piuccio commented May 8, 2017

Just stumbled upon this, I think the documentation need updating.

This example says the .catch will get called when status code is not 200 and there's no example of using .info()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants