sar*_*wc5 6 angular-http-interceptors angular angular-httpclient angular6
在阅读了有关http客户端错误处理的有关角度的文档后,我仍然不明白为什么我没有从服务器捕获带有以下代码的401错误:
export class interceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('this log is printed on the console!');
return next.handle(request).do(() => (err: any) => {
console.log('this log isn't');
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
console.log('nor this one!');
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在控制台日志上,我也得到了:
zone.js:2969 GET http:// localhost:8080 / test 401()
core.js:1449错误HttpErrorResponse {标题:HttpHeaders,状态:401,statusText:“确定”,url:“ http:// localhost:8080 / test ”,确定:否,…}
您的错误处理程序需要返回一个 new Observable<HttpEvent<any>>()
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<HttpEvent<any>>();
}));
Run Code Online (Sandbox Code Playgroud)
您应该使用捕获错误 catchError
return next.handle(request)
.pipe(catchError(err => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
console.log('this should print your error!', err.error);
}
}
}));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11613 次 |
最近记录: |