如何在HttpInterceptor上模拟HttpErrorResponse

时间:2019-06-07 21:58:14

标签: angular rxjs angular-httpclient angular-http-interceptors ngxs

我有一个HttpMockInterceptor类,以便在不操纵数据库中实际数据的情况下运行项目。

我将根据端点返回具有所需数据的HttpResponse,但是当我尝试发送HttpErrorResponse时,我的HttpService不会发出错误通知。

这是拦截器

let deco = combination(value1: 52, value2: 5)

Http服务

@Injectable()
export class HttpMockRequestInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<any> {
    const { url } = request;
    for (const petition of urls) {
      if (new RegExp(petition.url).test(url)) {
        const response = petition.response(request);
        const { default: payload, status } = response;
        console.log('Petition intercepted, loading from json:', petition.url);

        if (status === 404) {
          return of(
            new HttpErrorResponse({
              status: 404,
              error: 'Something Happened'
            })
          );
        }
        return of(
          new HttpResponse({
            status: 200,
            body: payload
          })
        );
      }
    }
    return next.handle(request);
  }
}

0 个答案:

没有答案