Angular httpClient interceptor error handling

前端 未结 5 1013
滥情空心
滥情空心 2021-02-15 23:45

after reading the documentation on angular about http client error handling, I still don\'t understand why I don\'t catch a 401 error from the server with the code below:

<
5条回答
  •  日久生厌
    2021-02-16 00:11

    here some example I'm using:

    export class ErrorHandlerInterceptor implements HttpInterceptor {
    
        intercept(
            request: HttpRequest,
            next: HttpHandler
        ): Observable> {
            const loadingHandlerService = this.inej.get(LoadingHandlerService);
            const errorHandlerService = this.inej.get(ErrorHandlerService);
    
            return next.handle(request)
                .pipe(
                    catchError(err => {
                        loadingHandlerService.hideLoading();
                        if (err instanceof HttpErrorResponse) { errorHandlerService.handleError(err) }
                        return new Observable>();
                    })
                );
        }
    
        constructor(private inej: Injector) { }
    }
    

提交回复
热议问题