3

I have a code to get request. It's compiling and working but I have an error in console.

public get<T>(url: string, headers: Headers = this.jsonHeaders()): Observable<T> {
        return this._http.get(url, { headers: headers })
                   .catch((err: Response, caught: Observable<T>) => {
                       if (err.status === 401) {
                           this._router.navigate(["/auth/login"]);
                           return Observable.throw("401 Unauthorized");
                       }
                       return caught;
                   })
                   .map(res => <T>this.toJSON(res));
    }

error:

error TS2345: Argument of type '(err: Response, caught: Observable<T>) => ErrorObservable | Observable<T>' is not assignable to parameter of type '(err: any, caught: Observable<Response>) => Observable<any>'.
  Types of parameters 'caught' and 'caught' are incompatible.
    Type 'Observable<Response>' is not assignable to type 'Observable<T>'.
      Type 'Response' is not assignable to type 'T'.

I tried cast it to but it's not helped

Illorian
  • 1,222
  • 2
  • 13
  • 38

1 Answers1

9

You use the following:

.catch((err: Response, caught: Observable<T>) => {
  if (err.status === 401) {
    this._router.navigate(["/auth/login"]);
    return Observable.throw("401 Unauthorized");
  }
  return Observable.throw(caught); // <-----
})
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360