I am currently learning Angular 6 and RXJS through using the HTTPClient.
The documentation https://angular.io/guide/http states that you can catch an error within the this.http.get method using pipe then tap like the example below lifted from the official docs.
getTextFile(filename: string) {
// The Observable returned by get() is of type Observable<string>
// because a text response was specified.
// There's no need to pass a <string> type parameter to get().
return this.http.get(filename, {responseType: 'text'})
.pipe(
tap( // Log the result or error
data => this.log(filename, data),
error => this.logError(filename, error)
)
);
}
I am trying to replicate this within my method like so
getLeads() : Observable<Lead[]> {
return this.http.get<Lead[]>('http://localhost:3000/leads').pipe(
tap (
error => console.log('error')
)
);
}
But it's not catching the error within tap(), however when using catchError like the below it does work.
getLeads() : Observable<Lead[]> {
return this.http.get<Lead[]>('http://localhost:3000/leads').pipe(
catchError(this.errorHandlerService.handleError('Could not get Leads', [])),
);
}
Is there a reason why the below is not working?
tap (
error => console.log('error')
)
tap, which gets the non-error result. That's not the same as the example.