After successfully implementing a code that called a logout() method upon receiving a 401 response from the server, I encountered issues following an upgrade of Angular from 5.2 to 7.0.3. It seems like either the HttpInterceptor interface has been modified or there have been numerous rxjs breaking changes. The current implementation appears as shown below and results in the error specified under the code snippet.
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();
}
})
);
}}
The TypeScript compiler throws the following error message:
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 => {