【发布时间】:2018-04-01 15:50:22
【问题描述】:
我有一个 HttpInterceptor 来捕获错误并以模式显示它们。除了错误代码和消息之外,我还想显示响应的正文,它实际上包含对错误的更精确描述(例如,关于 500 内部服务器错误)。 我怎样才能在角度上实现这一点? (我使用的是 4.3.6 版本。)
我已经查看了相关问题,但像 HttpErrorResponse._body 或类似的答案对我不起作用。 此外,在控制台中检查错误响应时,HttpErrorResponse.error 设置为 null。
这是我的拦截器当前的样子:
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
public constructor(private httpErrorService: HttpErrorService) { }
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).do(event => {
}, (error: HttpErrorResponse) => {
console.log('HTTPERROR INTERCEPTOR');
console.log(error);
if (error.status >= 400) {
this.httpErrorService.onError(error);
}
});
}
}
【问题讨论】:
标签: angular typescript http-error angular-http-interceptors