0
votes

I had a working code that called a logout() method in case of a 401 response from the server. As I have upgraded Angular from 5.2 to 7.0.3 it is not working anymore. I think it must be something with a modified HttpInterceptor interface or the many rxjs breaking changes. Right now it looks like this and throws the error below the code.

export class UnauthInterceptor implements HttpInterceptor {

  private session: SessionProvider;
  constructor(private injector:Injector) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      this.session = this.injector.get(SessionProvider);
      return next.handle(request).pipe(catchError(err => {
            if (err.status === 401) {
                // auto logout if 401 response returned from api
                this.session.logout();
            }
        })
      );
  }}

tsc throws the following error:

src/app/interceptors/unauth.interceptor.ts:19:51 - error TS2345: Argument of type '(err: any) => void' is not assignable to parameter of type '(err: any, caught: Observable>) => ObservableInput<{}>'.
Type 'void' is not assignable to type 'ObservableInput<{}>'.

19 return next.handle(request).pipe(catchError(err => {

1
You have to return an Observable from catchErrormartin

1 Answers

2
votes

Hello you have to return an observable with catchError like :

return throwError(error)