How to properly catch err 404 with loadRemoteImage?

When the app uses loadRemoteImage and an image is not found (which is OK, the image does not have to be there…) all the time I receive the following message in console on a yellow background:

WARNING: D:\a\corona\corona\platform\resources\init.lua:484: Failed to find image ‘user_8.png’

The listener handles errors quite well and even quits when it catches “err 404 message” generated by the server.

local function remote_image_listener_sub(event)
	for value_str, key_str in pairs(event.responseHeaders) do
		if value_str == "HTTP-STATUS-LINE" then
			if key_str == "HTTP/1.1 404 Not Found" then
			    -- works well!
				return
			end
		end
	end
	if (event.isError) then
		print("545: Network error - download failed: ", event.response)
			    -- hardly happens, but works well, too!
		return
	elseif (event.phase == "began") then
		print("Progress Phase: began")
	elseif (event.phase == "ended") then
		print("Progress Phase: downloaded!")
			    -- if there is no work around HTTP-STATUS-LINE,
			    -- then this part of code is executed.
			    -- The absence of file name indicates that the file
			    -- is not on the nework (err 404)!
		if event.response.filename == nil then
			display.remove(event.target)
			    -- yep, it works well!
			return
		end
			    -- here goes the rest of the code which
			    -- works with the freshly downloaded image...
	end
end

It seems like all the possible cases are handled well, but the listener still gives that message. I looked at the init.lua code, is it a bug there?

Considering that loadRemoteImage has a cache, maybe that message means “file not found locally, now I’ll try to download it”.

1 Like

UPDATED - I have to read questions more carefully.

  1. That does seem wrong. I have an OLD example here that loads remote images and it does not have any error or warning messages (using Solar2D 2021.3641)
    https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/remoteBackImage.zip

a. Try running my example and seeing if it has an error/warning.

  • If it does, try updating your Sola2D version.
  • Also, OS X or Windows? Maybe this is a Mac thing?

b. If my example works w/o errors/warnings, try editing the code to use your image(s), then run it again.

  • If there are no issues, see if I’m doing this differently than you.
  1. I don’t actually like that function much. I tend to use alternate means to download and fill images. These may or may not be helpful to you.
    https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2016/09
  • remoteFiles.zip
  • remoteFiles2.zip

Another example:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2020/09/downloadAndDisplayImagesWithCancel.zip

OFFTOPIC
While you didn’t ask, I thought I’d include this in case you find it useful…

I may be wrong, but I think event.status contains the response code (may be encoded as string).

In my games and for clients, I have a little module that checks for a bad error code:

local json = require "json"
--
local responses = {}
--
responses.checkCode = function( event, debugEn )
   local errorCode = tonumber(event.status)
   local gotError = false
   local badCodes = 
   { 
      301, 400, 401, 403, 404, 405, 406, 407, 408, 409, 410,
      411, 412, 413, 414, 415, 416, 422, 417, 500, 501, 502, 503, 
      504, 505
   }
   --
   gotError = gotError or ( tonumber(errorCode) > 300 )
   --
   if( debugEn and gotError) then
      print("\n\nERROR! - Error Code: ", errorCode, event.url, tostring(event.response) )
   end
   --
   return gotError, errorCode
end
--
return responses

Based on your code above, I’d use this module like this:

local responses = require "responses" -- file containing my code above
...
local function remote_image_listener_sub(event)
   local gotError, errorCode = responses.checkCode( event )
...
end

gotError will be true if there was a known error code.
errorCode will contain the numeric code value so you can act upon it if you need to.

In the case where you try to load a remote image and it does not exist, that seems like a reasonable warning.

I would avoid using loadRemoteImage if you can’t guarantee the image will be present.

That is, unless you can safely handle the image not drawing or at least not filling, I’d change the way you get those images. I provided alternatives above (in prior post).

Everything works well for me, I just do not want to see warning because it makes no sence to me. The function stops execution if ther is err404 or any other error, it also shows an alternative image beautifully applying transition to it… If I need such a warning I can print it to a console myself.

1 Like

Thank you for the sample @roaminggamer, I guess warning has to be removed from init.lua which is part of the Engine not the project. But, if it is impossible, then it is OK, I am not annoyed :nerd_face: