我需要拦截和修改一个 HttpResponse,它是 HttpErrorResponse 的实例,即状态:500,状态为 200 并填写其正文,以便我可以在我的 http.post 的 Observable 订阅中处理它作为成功的 http 调用。
最佳答案
我设法使用以下拦截器修改了 HttpErrorResponse:
export class CustomErrorHttpInterceptor implements HttpInterceptor {
constructor() {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError((httpErrorResponse: HttpErrorResponse) => {
// Customize as you wish:
const newHttpErrorResponse = new HttpErrorResponse( {
error: httpErrorResponse.error,
headers: httpErrorResponse.headers,
status: httpErrorResponse.status,
statusText: httpErrorResponse.statusText,
url: httpErrorResponse.url
});
return Observable.throw(newHttpErrorResponse);
})
);
}
}
关于Angular 8 拦截和修改Http错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59327417/