How do you catch with a pipe?

Nxt3 picture Nxt3 · Dec 1, 2017 · Viewed 26.9k times · Source

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.

Answer

AT82 picture AT82 · Dec 1, 2017

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);
  )