I'm building a firefox add-on using the add-on sdk. I need to make a http request to a certain page and I want to handle the connection timeout but couldn't find anything in the api: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/request.html
What I'm actually looking is a callback in case the client couldn't connect to the server.
Is there a way to achieve this?
The SDK request will always call
onComplete
, when the request is considered done for the network. This means thatonComplete
is called in any case, disregarding if the request returned an error or a success.In order to detect which error you've got, you need to check the Response object's (the object passed to the
onComplete
function) property "status" (response.status
). It holds the status code for the request. To look up status codes, consider the list on the mozilla developer network. If the response status is 0, the request has failed completely and the user is probably offline, or the target couldn't be reached.A timeout would either be a status code 504 or 0. The implementation would be similar to this:
I personally use a validation function on the request object, which returns me a number which depends whether I've got a correct response, an error from the web server or a connection issue (4xx and 0 status codes).