Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you catch with a pipe?

How do I do the following with lettable operators and pipe?

    this.httpClient
      .get(url)
      .map((res: any) => {
        const events = res.eventList;
        return events.map(e => new EventLogModel(e));
      })
      .catch(this.handleError);

I've tried this, but I can't get catchError to work: catchError does not exist on type Observable<any>:

    this.httpClient
      .get(url)
      .pipe(
        map((res: any) => {
          const events = res.eventList;
          return events.map(e => new EventLogModel(e));
        })
      )
      .catchError(this.handleError);

Also, I assume catch and catchError are the same, correct? I'm importing it like so:

import { map, catchError } from 'rxjs/operators';

but I wasn't sure if this was the correct operator to use.

like image 790
Nxt3 Avatar asked Dec 01 '17 19:12

Nxt3


People also ask

How do you catch an observable error?

Catch errors in the observable stream Another option to catch errors is to use the CatchError Operator. The CatchError Operators catches the error in the observable stream as and when the error happens. This allows us to retry the failed observable or use a replacement observable.

How do you handle errors in Mergemap?

You'll need to use retry or retryWhen (names are pretty self-explanatory) — these operators will retry a failed subscription (resubscribe to the source observable, once an error is emitted.

Does pipe subscribe?

The pipe method is for chaining observable operators, and the subscribe is for activating the observable and listening for emitted values. The pipe method was added to allow webpack to drop unused operators from the final JavaScript bundle. It makes it easier to build smaller files.


1 Answers

Your assumption is correct, the lettable operator catchError is the same as catch.

As for the placement of catchError, it should not have prefix . and should be placed within pipe:

this.httpClient
  .get(url)
  .pipe(
    map((res: any) => {
      const events = res.eventList;
      return events.map(e => new EventLogModel(e));
    }),
    catchError(this.handleError);
  )
like image 162
AT82 Avatar answered Oct 12 '22 02:10

AT82