Closed
Description
🐞 bug report
Affected Package
The issue is caused by package @angular/httpDescription
When you call an HTTP endpoint that returns a response without a status code, e.g. "400", but not "404 Not Found", Angular by default sets statusCode
to OK
which is weird and misleading.
🔬 Minimal Reproduction
import { Component } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor( http: HttpClient) {
http
.get('https://reqres.in/api/users/23')
.pipe(
catchError(this.handleError)
).subscribe();
}
private handleError(error: HttpErrorResponse) {
console.log(error.statusText); // Prints 'OK';
return null;
}
}
In the example above I am using a random public endpoint that doesn't return status text with status code: https://reqres.in/api/users/23
In Chrome console it looks like:
Request URL: https://reqres.in/api/users/23
Request Method: GET
Status Code: 404
Some investigation lead me to this file https://github.com/angular/angular/blob/7.2.0/packages/common/http/src/response.ts
But it looks like it should set default statusCode to 'Unknown Error', but it doesn't for some reason.
🌍 Your Environment
Angular Version:
Angular CLI: 6.1.5
Node: 9.6.1
OS: win32 x64
Angular: 6.1.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.7.5
@angular-devkit/build-angular 0.7.5
@angular-devkit/build-optimizer 0.7.5
@angular-devkit/build-webpack 0.7.5
@angular-devkit/core 0.7.5
@angular-devkit/schematics 0.7.5
@angular/cli 6.1.5
@ngtools/webpack 6.1.5
@schematics/angular 0.7.5
@schematics/update 0.7.5
rxjs 6.4.0
typescript 2.7.2
webpack 4.9.2