简体   繁体   中英

catch error from failed connection to named server sent event using rxjs fromEvent

im using rxjs fromEvent to connect to a named sse Event I want to catch an Error on connection loss

namedEvent() {
    try {
      const eventSource = new EventSource(
        'adress'
      );
      return fromEvent(eventSource, 'eventName').pipe(
        catchError((err) => {
          // handle the error
          console.log('Error:', err);
          return of(null);  // return an empty observable to prevent the stream from completing
        })
      );
    } catch (err) {
      // handle the error
      console.log('Error:', err);
      return of(null);  // return an empty observable to prevent the stream from completing
    }
  }

Im using try and catch block to catch an error on the connection init

and rxjs catchError to catch an error from the Observable

this does not return anything I only get the this error in my browser net::ERR_NAME_NOT_RESOLVED and on the.network tab failed to load response data

how can I catch this error?

You can subscribe to fromEvent:

return fromEvent(eventSource, 'eventName').pipe(
        // Do whatever
      ).subscribe({
          error(err){
             console.error(err.message);
          }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM