404 error

I get the url of an image from an api call. Well it turns out there is no image at that url. Brightscript doesn’t error out, it keeps running but now everything is screwed up. So, is there anyway to test for a 404? Anyway to test image size before displaying it? Any suggestions at all?

You should use the roSystemLog http://sdkdocs.roku.com/display/sdkdoc/roSystemLog to capture the 404 result, unless you think that it’s something that will happen often.

If you want to be more proactive rather than reactive, use http://sdkdocs.roku.com/display/sdkdoc/ifUrlTransfer#ifUrlTransfer-HeadasDynamic to do HEAD on the URL. This will give you the 404 if it’s missing, and if it’s found it will not attempt to download the file.

You didn’t say which API you’re using, but roUrlTransfer.GetToFile returns the HTTP status code, and the Async methods give the status code in the roUrlEvent.

–Mark

Function check(thisurl) as string
request = CreateObject(“roUrlTransfer”)
port = CreateObject(“roMessagePort”)
request.SetMessagePort(port)
request.SetUrl(thisurl)
if (request.AsyncGetToString())
while (true)
msg = wait(0, port)
if (type(msg) = “roUrlEvent”)
code = msg.GetResponseCode()
if (code = 200)
print"good"
return “good”

elseif (code =400 or code=404)
print(“image not found”)
return “bad”
else
print"error"; code
return “generic bad code”

endif
else if (event = invalid)
request.AsyncCancel()
endif
end while
endif
return invalid
End Function