How to use the @angular/common/http.HttpErrorResponse function in @angular/common

To help you get started, we’ve selected a few @angular/common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github usgs / earthquake-eventpages / src / app / tell-us / form-submit.service.spec.ts View on Github external
latitude: 35,
      longitude: -105
    };

    // Success Response
    feltResponseSucces = {
      ciim_mapLat: '35',
      ciim_mapLon: '-105',
      ciim_time: 'five minutes ago',
      eventid: '1234abcd',
      form_version: '1.2.3',
      your_cdi: 'I'
    };

    // Error Response
    feltResponseError = new HttpErrorResponse({
      status: 500,
      statusText: 'Server Error'
    });

    it('handles successes', inject(
      [FormSubmitService],
      (service: FormSubmitService) => {
        service.onSubmit(feltReport);
        const req = httpClient.expectOne(service.responseUrl);
        req.flush(feltResponseSucces);

        service.formResponse$.subscribe(response => {
          expect(response).toEqual(feltResponseSucces);
        });
      }
    ));
github lealceldeiro / gms / client / src / app / core / interceptor / all-requests.interceptor.spec.ts View on Github external
() => {
      const error: HttpErrorResponse = new HttpErrorResponse({ status: HttpStatusCode.UNAUTHORIZED, statusText: 'Server Error', url: url });
      httpClient.get(url).subscribe(() => { }, () => {
        // error
        expect(loaderServiceSpy.start).toHaveBeenCalledTimes(1);
        expect(loaderServiceSpy.stopAll).toHaveBeenCalledTimes(1);
      });
      httpTestingController.expectOne(url).flush({}, error);
    });
});
github VadimDez / ng2-pdf-viewer / build / lib / @angular / common / @angular / common / http / testing.js View on Github external
status = 204;
                statusText = statusText || 'No Content';
            }
            else {
                statusText = statusText || 'OK';
            }
        }
        if (statusText === undefined) {
            throw new Error('statusText is required when setting a custom status.');
        }
        if (status >= 200 && status < 300) {
            this.observer.next(new HttpResponse({ body, headers, status, statusText, url }));
            this.observer.complete();
        }
        else {
            this.observer.error(new HttpErrorResponse({ error: body, headers, status, statusText, url }));
        }
    }
    /**
github infra-geo-ouverte / igo2-lib / projects / core / src / lib / request / error.interceptor.ts View on Github external
private handleError(httpError: HttpErrorResponse, req: HttpRequest) {
    const msg = `${req.method} ${req.urlWithParams} ${httpError.status} (${
      httpError.statusText
    })`;

    if (httpError instanceof HttpErrorResponse) {
      const errorObj = httpError.error === 'object' ? httpError.error : {};
      errorObj.message = httpError.error.message || httpError.statusText;
      errorObj.caught = false;
      console.error(msg, '\n', errorObj.message, '\n\n', httpError);

      this.httpError = new HttpErrorResponse({
        error: errorObj,
        headers: httpError.headers,
        status: httpError.status,
        statusText: httpError.statusText,
        url: httpError.url
      });
    }

    return throwError(this.httpError);
  }
github tomastrajan / angular-ngrx-material-starter / projects / angular-ngrx-material-starter / src / app / features / examples / stock-market / stock-market.reducer.spec.ts View on Github external
it('should reflect the Error that occured', () => {
      const error = new HttpErrorResponse({});
      const action = actionStockMarketRetrieveError({ error: error });
      const state = stockMarketReducer(originalState, action);

      expect(state.symbol).toBe(state.symbol);
      expect(state.loading).toBeFalsy();
      expect(state.stock).toBeNull();
      expect(state.error).toBe(error);
    });
  });
github Alfresco / alfresco-ng2-components / lib / process-services-cloud / src / lib / process / start-process / services / start-process-cloud.service.spec.ts View on Github external
it('should not be able to get all the process definitions if error occurred', () => {
        const errorResponse = new HttpErrorResponse({
            error: 'Mock Error',
            status: 404, statusText: 'Not Found'
        });
        spyOn(service, 'getProcessDefinitions').and.returnValue(throwError(errorResponse));
        service.getProcessDefinitions('appName1')
            .subscribe(
                () => {
                    fail('expected an error, not applications');
                },
                (error) => {
                    expect(error.status).toEqual(404);
                    expect(error.statusText).toEqual('Not Found');
                    expect(error.error).toEqual('Mock Error');
                }
            );
    });
github maxime1992 / angular-ngrx-starter / src / app / shared / helpers / mock.helper.ts View on Github external
export function responseBody(
  body: T,
  status = 200,
  error?: { code: number; message: string }
): Observable {
  if (status >= 200 && status < 300) {
    return of(body).pipe(delay(environment.httpDelay));
  } else {
    return _throw(new HttpErrorResponse({ status, error })).pipe(
      materialize(),
      delay(environment.httpDelay),
      dematerialize()
    );
  }
}
github tomastrajan / angular-ngrx-material-starter / projects / angular-ngrx-material-starter / src / app / features / examples / stock-market / components / stock-market-container.component.spec.ts View on Github external
beforeEach(() => {
        store.setState(
          createState({
            symbol: 'TDD',
            loading: false,
            error: new HttpErrorResponse({})
          })
        );
        fixture.detectChanges();
      });
github patrickmichalina / fusebox-angular-universal-starter / src / client / app / shared / transfer-http / transfer-http-interceptor.service.ts View on Github external
intercept(req: HttpRequest, next: HttpHandler): Observable> {

    const cachedHttpEvent = this.transferState.get | HttpErrorResponse | undefined>(this.createCacheKey(req), undefined)

    if (cachedHttpEvent) {
      if (cachedHttpEvent.status < 200 || cachedHttpEvent.status >= 300) {
        const err = cachedHttpEvent as HttpErrorResponse
        const cachedErrorResponse = new HttpErrorResponse({
          status: err.status,
          statusText: err.statusText,
          error: err.error
        })
        return Observable.throw(cachedErrorResponse)
      } else {
        const response = cachedHttpEvent as HttpResponse
        const cachedResponse = new HttpResponse({
          body: response.body,
          status: response.status,
          statusText: response.statusText
        })
        return Observable.of(cachedResponse)
      }
    }