Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript try-catch doesn't catch 'Failed to load resource: net::ERR_CONNECTION_RESET '

I have a multiple file uploader but while it's uploading sometimes 1 out of 10 files doesn't make it and it returns a Failed to load resource: net::ERR_CONNECTION_RESET in chrome console. I tried to catch it with the try-catch, but it acts as if no error occurred. What am I doing wrong?

var ajax = new XMLHttpRequest();
ajax.open("POST", "/multiFileUploadHandler.php");
try {
    ajax.send(formdata);
} catch (err) {
    alert('Error: '+err);
}
like image 632
Matthew Abrman Avatar asked Mar 05 '14 10:03

Matthew Abrman


1 Answers

This is likely because it is async. Try catching this with an onerror event handler.

ajax.onerror = function(error) {
    // handle error
};

edit: corrected syntax.

like image 129
Evil Buck Avatar answered Sep 23 '22 18:09

Evil Buck