I have a webpage that attempts to set the src of an image. I fully expect errors depending on what image I ask for, and I want to catch the error messages and have different responses depending on which error I get.
I am looking to compare net::ERR_CONNECTION_REFUSED vs net::ERR_NAME_NOT_RESOLVED.
I tried looking at the error event, but there doesn't appear to be any information about the actual error message or type in the event.
both events look like this
Event {isTrusted: true, type: 'error', target: img, currentTarget: img, eventPhase: 2, …}
isTrusted: true
bubbles: false
cancelBubble: false
cancelable: false
composed: false
currentTarget: null
defaultPrevented: false
eventPhase: 0
path: (5) [img, body, html, document, Window]
returnValue: true
srcElement: img
target: img
timeStamp: 2342.2000000178814
type: "error"
I would do a try/catch as that seems to return the error message as a string, but I'm not sure how to actually catch it, as the error is on the HTTP request, not the src change.
update
I tried accessing the path with an XMLHttpRequest, and catching the send, but I don't get the error message in the try/catch statement doing that. The debugger shows the error is at h.send() but the catch statement is not executed.
example
try{
h=new XMLHttpRequest();
h.open("GET","https://address");
h.send();
}catch (e){
console.log("error");
console.log("ERROR:",e);
}