spring-projects / spring-projects/spring-boot
ResponseStatusException thrown from method with @ExceptionHandler annotation always return status 500
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 81.5k
- Forks
- 42.7k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 65
Description
Spring Boot Version: 2.5.0
I'm trying to throw an instance of ResponseStatusException with a status code on a method annotated with @ExceptionHandler, but it is always returning status 500.
What I'm trying to do:
Obs: Failure is a custom exception of my project with an error message and error status code.
@RestController
class LoginController(
@Autowired private val loginUseCase: ActionUseCase<LoginRequest, LoginResponse>
) {
@PostMapping("/login")
fun login(
@RequestBody loginRequest: LoginRequest
): LoginResponse {
return loginUseCase.execute(
loginRequest
)
}
@ExceptionHandler(Failure::class)
fun handleException(failure: Failure) {
throw ResponseStatusException(failure.errorStatus, failure.message)
}
}
When the method execute throws a Failure, the response I was expecting is:
{
"timestamp": "2021-05-31T00:32:29.211+00:00",
"status": 400,
"error": "Bad Request",
"message": "reason",
"path": "path"
}
But I'm always getting:
{
"timestamp": "2021-05-31T00:32:29.211+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "reason",
"path": "path"
}
I know that I can use @ResponseStatus annotation on my custom exception and I'm already using that, but I want to report this because I think it's not an expected behavior.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Spring Boot 2.5.0 handling path for @ExceptionHandler and ResponseStatusException, reproducing the LoginController example with Failure. Verify whether the handler's thrown exception is converted to the requested 400 status; done means the behavior is fixed or its expected limitation is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100