Как смоделировать HttpErrorResponse на HttpInterceptor - PullRequest
0 голосов
/ 08 июня 2019

У меня есть класс HttpMockInterceptor для запуска моего проекта без манипулирования реальными данными в базе данных.

Я возвращаю HttpResponse с нужными данными в зависимости от конечной точки, но когда я пытаюсьчтобы отправить HttpErrorResponse, мой HttpService не уведомляет об ошибке событие.

Это перехватчик

@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);
  }
}

Служба Http

    getUniversityByID(universityID: number) {
    const path = `/university/find/${universityID}`;

    this.sharedHttp.get(path).subscribe(
      (res: Universities) => {

        // Returning HtppResponse works...
        this.store.dispatch(new GetUniversitySuccess(res));
      },
      err => {

        // Returning HtppErrorResponse don't works...
        this.store.dispatch(new GetUniversityError(err.message));
      }
    );
  }
...