1

I have a web application with Springboot and Angular 6.

A response entity with a detailed error message does not show the right error in Angular (instead of "Invalid Excel file" displays "Http failure response for http://localhost:8081/api/v1/activities/excel/import: 500 OK")

Controller's code:

...
@PostMapping("/activities/excel/import")
public ResponseEntity<?> import(...) {
    return new ResponseEntity<> ("Invalid Excel file", HttpStatus.BAD_REQUEST);
}
...

UI (Angular 6):

importFromExcel() {
    this.activityService.importFromExcel(this.file).subscribe(
        (result) => {...},
        (error) => {
            console.log (error.error) //displays undefined
            console.log (error.name) //displays undefined
            console.log (error.message) //displays  undefined
            console.log(JSON.stringify(error)) //displays Http failure response for http://localhost:8081/api/v1/activities/excel/import: 500 OK 
        }
    )
}         

How can I retrieve the HTTP response body?


PROBLEM SOLVED
An interceptor (which have been added in other parts of the code) was handling error messages from backend in an incomplete way. As a result in some places in the app (like my example) the error messages were no longer displayed. Problem identified :).

Florin Laudat
  • 51
  • 1
  • 5
  • Please, add the output of `JSON.stringify(error)` and `typeof error` to the question – Vusal Feb 05 '19 at 09:08
  • 1
    @Vusal : Thank you for your help. The result on the JSON(stringify(error)) was "Http failure response for http://localhost:8081/api/v1/activities/excel/import: 500 OK" - see the description. – Florin Laudat Feb 06 '19 at 13:43

1 Answers1

1

You can retrieve an http response body in the next way:

this.activityService.importFromExcel(this.file).subscribe(
  data => console.log('success', data),
  error => console.log('oops', error.error)
);

The error.error should contain the HTTP response body.

Please check the next links for more information:

  1. Catching errors in Angular HttpClient
  2. Angular 6: How to set response type as text while making http call
  3. HttpErrorResponse#error is not instance of Error in client-side/network error
Vusal
  • 2,138
  • 10
  • 23