display.loadRemoteImage issue when 403 or 404 returned

display.loadRemoteImage() has some bad unexpected behavior. First off look at the docs and try to run that code(http://docs.coronalabs.com/api/library/display/loadRemoteImage.html). The remote server returns a 404 error and the API has no idea how to handle it. The code crashes and never hits the event listener, it tries to load the filename given in the API call(helloCopy.png). This shouldn’t happen, if the remote server throws a 403,404 or any error display.loadRemoteImage should still hit the listener. The way it is now, there is nothing you can do about these errors. I can evaluate if event.status == 403 but even when that happens the API wants to load the default image name passed in the API call.

Here is the code example from the Docs, the remote image isn’t there (404 error) so this code crashes because its trying to load the “helloCopy.png” image instead of going to the listener.

local function networkListener( event ) if ( event.isError ) then print ( "Network error - download failed" ) else event.target.alpha = 0 transition.to( event.target, { alpha = 1.0 } ) end print ( "RESPONSE: " .. event.response ) end display.loadRemoteImage( "http://www.coronalabs.com/demo/hello.png", "GET", networkListener, "helloCopy.png", system.TemporaryDirectory, 50, 50 )

Just tried to do the same thing with network.download and pretty much the same result. Am I missing something? Can these calls work on the simulator? I am starting to think my IDE is having something to do with the issue.

Okay,  while a 404 error (file not found) is an error, it’s not the kind of error that we are looking for.  Technically, the web server returned a result, so the .isError check in the code above is actually checking for things like the server not being reachable.

You need to check event.status to make sure its under 400.  You shouldn’t get any 100 errors since you’re not putting any data.  200’s are success, 300’s are some type of redirect which should end up with a 200 code.  400’s and 500’s indicate some kind of failure, like 403 access denied, 404 file not found, 500 is usually the service is down type problem.   You can’t look for just “200”, you have to look for any valid 200 code so (math.floor(event.status) == 2) would be a way to check any 200 codes.

I changed the if event.isError line to this:

if ( event.isError or event.status > 399 ) then

as a quick and dirty way to look for the 400 and 500 type codes.

Just tried to do the same thing with network.download and pretty much the same result. Am I missing something? Can these calls work on the simulator? I am starting to think my IDE is having something to do with the issue.

Okay,  while a 404 error (file not found) is an error, it’s not the kind of error that we are looking for.  Technically, the web server returned a result, so the .isError check in the code above is actually checking for things like the server not being reachable.

You need to check event.status to make sure its under 400.  You shouldn’t get any 100 errors since you’re not putting any data.  200’s are success, 300’s are some type of redirect which should end up with a 200 code.  400’s and 500’s indicate some kind of failure, like 403 access denied, 404 file not found, 500 is usually the service is down type problem.   You can’t look for just “200”, you have to look for any valid 200 code so (math.floor(event.status) == 2) would be a way to check any 200 codes.

I changed the if event.isError line to this:

if ( event.isError or event.status > 399 ) then

as a quick and dirty way to look for the 400 and 500 type codes.