Search code examples
angularrxjsobservablerxjs-lettable-operators

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.


Solution

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