1

I'm trying to solve another Angular problem - can you help me? In my component I have a login function - like this:

    onLogin() {
    if (this.loginForm.valid) {
      const email = this.loginForm.get(['email']).value;
      const password = this.loginForm.get(['password']).value;

      this.ls.login(email, password).subscribe(
        result => {
          this.saveToken(result);
        },
        error => {
          this.errorMessage = error;
          this.loginForm.reset();
        }
    );

    } else {
      this.updateErrorMessage();
    }
  }

My service function looks like that:

login(email: string, password: string): Observable<string> {

    const apiUrl = environment.apiHostLumen + '/admin/login?email=' + email + '&password=' + password;

    const headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    const options = new RequestOptions({ headers: headers });

    return this.http.get(apiUrl, options).pipe(
                    retry(3),
                    map(res => res.json()),
                    map(data => {
                      return data.token;
                    }),
                    catchError(this.ehs.handleError('login'))
    );

  }

Now I have implemented the function for error handling exactly as described in the documentation of Angular. I have outsourced the feature and it looks like that:

handleError<T> (operation = 'operation', result?: T) {
    return (error: HttpErrorResponse): Observable<T> => {

      const userMessage = error.message;

      return throwError(userMessage);
    };
  }

But how it's possible to access the body of the API response? error.message is always undefined. I only have access to the status code and url. How can I access the response body if the API returns an error code different from 200 ?

Thanks a lot

3
  • Typescript tells me error.json() is not available for type HttpErrorResponse Commented Jun 20, 2018 at 9:07
  • can you share your server side code, there might be case its returning error object without message Commented Jun 20, 2018 at 9:08
  • I'm voting to close this question as its already answered by OP Commented Jun 20, 2018 at 9:19

3 Answers 3

3

You can use a HttpInterceptor for this. From that you can handle all your http errors in a single class. You can read more about Intercepting requests and responses in the angular official doumetation.

And by using intercept method you can intercept each and every request and response.

 public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next
      .handle(req)
      .pipe(catchError(err => {
        if (err instanceof HttpErrorResponse) {
          this.onError(err);
        }
        return throwError(err);
      }));
  }



 private onError(response: HttpErrorResponse): void {
    const status: number = response.status;
    const serverErrorMessage = response.error;

    switch(status) {
      case 400:
        console.log("Bad request"); //or you can print the serverErrorMessage
      case 404:
        console.log("Not found");
    }
  }
Sign up to request clarification or add additional context in comments.

3 Comments

that is not needed in my application i get details without doing this stuff
But you have to handle the error of every request by your self. Same code will be repeated agin and again. That's not a good practice. When you implement a HttpInterceptor you don't have to do that. It will be handled by the interceptor. It will save your time.
I have found the mistake, but nevertheless I will read in on this topic - it sounds interesting
1

You can do error.error, which should give you the error response from the server.

handleError<T> (operation = 'operation', result?: T) {
    return (error: HttpErrorResponse): Observable<T> => {

      const userMessage = error.error; // should give error body

      return throwError(userMessage);
    };
}

Sources :

https://angular.io/api/common/http/HttpErrorResponse

Accessing HTTP Error Response Body from HttpInterceptor in Angular

Comments

1

I have found the mistake. Many thanks for your help! I renamed the error type to any and with error.json() I can actually access the response body as normal. I do not know how I could overlook that ...

Thanks! It seems to work!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.