Search code examples
javascriptangulartypescriptangular-httpclient

Angular's subscribe not logging full error


I'm trying to log an error from a subscribe, but the error seems... incomplete?

I've never seen this kind of problem, nor could I find anything on Google. The code is the following:

this._http.post(this.urlPath, email).subscribe(
    res => {
      // stuff
    },
    err => {
      console.log(err) // <- I need to log this
    }
  );

It works to an extent. When I open the browser's console, in order to check the error, what I get is this:

enter image description here

The thing is, it seems like there's missing information here. If I open the Network tab, the response for this same request looks like this:

enter image description here

As you can see, the real response has more information. I've tried using {observe: "response"}, but to no avail. I should note that if I try using fetch, the response comes complete, but I'd rather use HttpClient.

What is going on here?


Solution

  • When you receive a http error status code you can't access to the payload returned by the service by the same way that in a success case. Is like an special object.

    But you can acccess to it doing some like this, using a pipe in your service and an error handler. This is a minimal example of it:

    your.service.ts
    ...
    
    handleError(error) {
      return throwError(error.error);
    }
    return  this.http.get ... the rest of your request.pipe(
     catchError(this.handleError)
    );
    ...
    

    And where you consume your service, in err you can acces to full response that your error request contains.

     ...
     , error => {
       console.warn(error);        
     }
    

    Or better than, you can throw the entire object to access to the error (response body) and the rest of params, like status code.