Skip to content

ResponseStatusException threw on method with @ExceptionHandler annotation always return status 500 #26715

Open
@manoellribeiro

Description

@manoellribeiro

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions