I recently attempted to incorporate catch handling within a service, however, I encountered an issue with an undefined function error displaying in my console.
Here is the specific error message that appeared:
TypeError: rxjs_internal_Observable__WEBPACK_IMPORTED_MODULE_2__.Observable.throw is not a function
import { Injectable } from '@angular/core';
import { config } from '../../../config/config';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { HttpResponseHandler } from './http-response-handler.service';
import { Observable } from 'rxjs/internal/Observable';
@Injectable()
export class DataService<T> {
constructor(
protected url: string,
protected httpClient: HttpClient,
protected responseHandler: HttpResponseHandler
) { }
getOneById<T>(id): Observable<T> {
return this.httpClient
.get<T>(config.ecmBackUrl + this.url + '/' + id)
.pipe(catchError((err, source) => this.responseHandler.onCatch(err, source)));
}
}
@Injectable()
export class HttpResponseHandler {
public onCatch(response: any, source: Observable<any>): Observable<any> {
console.log(response);
}
}