1

I've created a service for processing a user login (by hitting a backend server which responds with a JWT on successful authentication).

  login(username: string, password: string): Observable<boolean> {

    const headers = new Headers({
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxx'
    });
    const options = new RequestOptions({headers: headers});

    const body = new URLSearchParams();
    body.set('username', username);
    body.set('password', password);
    body.set('grant_type', 'password');

    return this.http.post('http://localhost:8080/api/oauth/token', body, options)
      .map((response: Response) => {
        if (response.json() && response.json().access_token) {
          window.localStorage.setItem('username', username);
          window.localStorage.setItem('id_token', response.json().access_token);
          window.localStorage.setItem('refresh_token', response.json().refresh_token);
          return true;
        } else {
          return false;
        }
      })
      .catch(this.handleError);
  }

It works, in that it successfully performs a post request to the backend server and correctly retrieves a token which is then usable for authenticating future requests. However, in the case where the user inputs an incorrect password, the server responds with a 400 status. Though I am catching that status (which I've verified by logging some info inside "handleError", Angular is ALSO printing the default POST error to the console. Specifically:

zone.js:2224 POST http://localhost:8080/api/oauth/token 400 ()

How do I stop it from doing this? I'm catching the error, handling it appropriately but still zone.js prints the error to Chrome console.

P1xt
  • 324
  • 1
  • 11
  • It might just be the way Chrome works. See if this answers your question: https://stackoverflow.com/questions/41660998/angular2-http-catch-error-and-avoid-error-output-to-console – Mark Rajcok Aug 04 '17 at 02:35

0 Answers0