How can I simulate HTTP error response in Angular service? I often need to handle different HTTP error codes and sometimes I need implement solution, but backend is not ready. How can I mock errors from backend? Example code
  public getData(): Observable<Response> {
    return this.httpClient.get<Response>(`${this.endpoint}`);
  }
                Simplest solution - you can create an HttpErrorResponse instance, set error status and then return this object as service fake response.
  public getData(): Observable<Response> {
    const error = new HttpErrorResponse({ status: 422 });
    return of(error) as any;
  }
or 
  public getData(): Observable<Response> {
    const error = new HttpErrorResponse({ status: 422 });
    return throwError(error) as any;
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With