angular: (bug): httpClient is automatically parsing error responses to string since 4.4.0-RC.0

I’m submitting a…


[ X ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When a post (I’ve only tested this method) request returns an error, like this:

{
  "base": [{
    "error": "something invalid"
  }]
}

… the error is parsed to string even the server returns a valid JSON as above.

Expected behavior

The error response shouldn’t be parsed to string.

Minimal reproduction of the problem with instructions

http://plnkr.co/edit/IjmDdiM0BwFJlpk1JLkR?p=info

Just click “Send post” and take a look in the console (specifically in error… it’s parsed to string).

Environment

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.4.0
node: 8.1.1
os: linux x64
@angular/animations: 4.4.0-RC.0
@angular/cdk: 2.0.0-beta.10
@angular/common: 4.4.0-RC.0
@angular/compiler: 4.4.0-RC.0
@angular/core: 4.4.0-RC.0
@angular/forms: 4.4.0-RC.0
@angular/http: 4.4.0-RC.0
@angular/material: 2.0.0-beta.10
@angular/platform-browser: 4.4.0-RC.0
@angular/platform-browser-dynamic: 4.4.0-RC.0
@angular/platform-server: 4.4.0-RC.0
@angular/router: 4.4.0-RC.0
@angular/cli: 1.4.0
@angular/compiler-cli: 4.4.0-RC.0
@angular/language-service: 4.4.0-RC.0
typescript: 2.5.2

Others: It was working on 4.3.6 as you can see here.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 11
  • Comments: 16 (3 by maintainers)

Most upvoted comments

I’m using Angular 5.0.0 - but also having this error

Version 6.4.2, still have the issue. When do you plan to fix it?

Issue is still broken in 4.4.4

Here is a workaround, create a response interceptor with:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).catch(event => {
      if (event instanceof HttpErrorResponse) {
        const response = event as HttpErrorResponse;
        if (response.headers.get('content-type') === 'application/json') {
          return Observable.throw(new HttpErrorResponse({
            error: JSON.parse(response.error),
            headers: response.headers,
            status: response.status,
            statusText: response.statusText,
            url: response.url,
          }));
        }
      }
      return Observable.throw(event);
    })
}

Same issue with 4.4.3

Looks like a bug to me, likely due to https://github.com/angular/angular/commit/452a7ae88b3b1144369f884c10de044502dc3c65 Since we’re now handling all the JSON parsing but we aren’t parsing the response in either position https://github.com/angular/angular/blob/master/packages/common/http/src/xhr.ts#L210 or https://github.com/angular/angular/blob/master/packages/common/http/src/xhr.ts#L225 where those would have been parsed by the browser previously

I’m also waiting for this to be resolved.