title: Angular handleError: How to get custom API error messages?
body: <p>I'm trying to solve another Angular problem - can you help me? In my component I have a login function - like this:</p>
  
  <pre><code>    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 =&gt; {
            this.saveToken(result);
          },
          error =&gt; {
            this.errorMessage = error;
            this.loginForm.reset();
          }
      );
  
      } else {
        this.updateErrorMessage();
      }
    }
  </code></pre>
  
  <p>My service function looks like that:</p>
  
  <pre><code>login(email: string, password: string): Observable&lt;string&gt; {
  
      const apiUrl = environment.apiHostLumen + '/admin/login?email=' + email + '&amp;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 =&gt; res.json()),
                      map(data =&gt; {
                        return data.token;
                      }),
                      catchError(this.ehs.handleError('login'))
      );
  
    }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>handleError&lt;T&gt; (operation = 'operation', result?: T) {
      return (error: HttpErrorResponse): Observable&lt;T&gt; =&gt; {
  
        const userMessage = error.message;
  
        return throwError(userMessage);
      };
    }
  </code></pre>
  
  <p><strong>But how it's possible to access the body of the API response?</strong> <code>error.message</code> is always undefined. I only have access to the <code>status</code> code and <code>url</code>. How can I access the response body if the API returns an error code different from <code>200</code> ?</p>
  
  <p>Thanks a lot</p>