javascriptjavaspring-bootexceptionaxios

Axios statusText is always empty


I've been using axios recently but I can't get the error message thrown by Spring Boot RuntimeException.

I've created a simple RuntimeException which throws 404 status:

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {
    public BadRequestException(String message) {
        super(message);
    }
}

I make a GET request to one of my endpoint where all it does is throw the BadRequestException with some message:

@RestController
public class TestController {

    @GetMapping("/throw")
    public ResponseEntity<?> throwError() {
        throw new BadRequestException("This text should be in statusText");
    }
}

console.log() from the catch clause (.catch((error) => console.log(error.response))) is: enter image description here

As you can see, the statusText is just an empty string, where "This text should be in statusText" should be expected.

Using fetch() works perfectly fine, but switching from axios to fetch in my situation will be time-wasting.

Am I missing something? Do I even throw the exceptions with my messages correctly?

Thanks in advance for all answers!


Solution

  • The reason is Spring Boot set a server.error.include-message as never by default. Setting it to always returns the message in a response.