Angular httpclient 4.3重写响应状态从200到500

时间:2017-11-26 00:34:43

标签: httpclient angular-http-interceptors

我的服务器总是以200状态响应,但在正文中响应errorCode。我想拦截该响应并将状态重写为403或500.如何使用新的http拦截器执行此操作?这是迄今为止的代码

@Injectable()
export class ApiInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): 
Observable<HttpEvent<any>> {

    return next.handle(req)
      .map((event: HttpEvent<any>) => {
        if (event instanceof HttpResponse  && (event.body.errorCode)) {
          return event.clone({status: 500})
        }  
      });
   }
}

0 个答案:

没有答案