Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What error is thrown when trying to appendChild script element fails due to unauthorized

I want to catch a specific failure of this JavaScript code:

var script = $wnd.document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');

When the url where the script resides needs the user to be logged in, and so returns an HTTP 401 Unauthorized error.

None of the values I understand that error (in a try/catch) can take on seem to match very well.

EvalError: An error in the eval() function has occurred.
RangeError: Out of range number value has occurred.
ReferenceError: An illegal reference has occurred.
SyntaxError: A syntax error within code inside the eval() function has occurred. event.
TypeError: An error in the expected variable type has occurred.
URIError: An error when encoding or decoding the URI has occurred (ie: when calling encodeURI()).

Is there any way to catch specifically this 401 error, or at least the class of IO error that would be thrown by not being able to load the script.

Thanks

like image 353
Andrew Mackenzie Avatar asked Dec 06 '25 05:12

Andrew Mackenzie


1 Answers

script.addEventListener('error', function(){
    // Didn't load
}, true);
like image 164
Eric Avatar answered Dec 08 '25 17:12

Eric