How can I catch the http status code of a failed EventSource connection?
function onError(event) {
console.log('request status code?');
}
const source = new EventSource('url', { withCredentials: true });
source.addEventListener('error', onError);
Use a regular try catch.
function onError(event) {
console.log('request status code?');
}
try {
const source = new EventSource('url', { withCredentials: true });
source.addEventListener('error', onError);
} catch(error) {
console.log(error)
}
EventSource will throw an exception if the connection fails (HTTP error). The onError is for errors that have happened after the connection was successful, which will not throw an exception but will execute the onError callback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With