spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Inconsistent error response when problemdetails enabled

Open jonatan-ivanov opened this issue 2 years ago • 1 comments

Affects: Boot 3.0.1, Framework 6.0.3

With the current version (Boot 3.0.1, Framework 6.0.3), if Problem Details are enabled (spring.mvc.problemdetails.enabled=true) and if I throw out an error from an MVC controller method, sometimes I get a Problem Details response, sometimes I get the "old" (Boot 2.x, Framework 5.x) response format depending on the default error handler "knows" the exception or not.

@GetMapping("/")
public String trouble() {
	// throw new IllegalStateException("Noooooo!"); // "old" (5.x) response
	throw new ResponseStatusException(INTERNAL_SERVER_ERROR, "Noooooo!"); // -> problem details response
}

When I throw IllegalStateException

HTTP/1.1 500
Connection: close
Content-Type: application/json
Date: Wed, 18 Jan 2023 21:34:42 GMT
Transfer-Encoding: chunked

{
    "error": "Internal Server Error",
    "path": "/",
    "status": 500,
    "timestamp": "2023-01-18T21:34:42.764+00:00"
}

When I throw ResponseStatusException

HTTP/1.1 500
Connection: close
Content-Type: application/problem+json
Date: Wed, 18 Jan 2023 21:35:04 GMT
Transfer-Encoding: chunked

{
    "detail": "Noooooo!",
    "instance": "/",
    "status": 500,
    "title": "Internal Server Error",
    "type": "about:blank"
}

I think the expected behavior would be a consistent response format; application/problem+json if problem details enabled and the old (5.x) application/json format if not.

jonatan-ivanov avatar Jan 18 '23 21:01 jonatan-ivanov

We've added support for ProblemDetail in #32634 with a configuration flag. This support was brand new at the time and we wanted to introduce it progressively to avoid too much user facing changes in Spring Boot 3.0. Right now the Spring Boot error handling format does not use the ProblemDetail format and we will expand the support in #19525, and possibly revisit more generally our error handling support in light of what's supported now in Spring Framework.

bclozel avatar Jan 19 '23 09:01 bclozel