Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServerSentEvents - Http Response Status Code

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);
like image 346
Hitmands Avatar asked Jul 04 '26 03:07

Hitmands


1 Answers

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.

like image 174
Yftach Avatar answered Jul 06 '26 16:07

Yftach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!