Angular httpClient interceptor error handling

前端 未结 5 978
滥情空心
滥情空心 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:24

    Your error handler needs to return a new Observable>()

    return next.handle(request)
        .pipe(catchError((err: any) => {
            console.log('this log isn't');
            if (err instanceof HttpErrorResponse) {
                if (err.status === 401) {
                    console.log('Unauthorized');
                }
            }
    
          return new Observable>();
        }));
    

提交回复
热议问题