Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ResponseEntityExceptionHandler creates inconsistent error formats for different errors #35701

Closed
francos opened this issue Jun 2, 2023 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@francos
Copy link

francos commented Jun 2, 2023

When using ResponseEntityExceptionHandler, by either creating a parent class

@ControllerAdvice
public class ErrorHandler extends ResponseEntityExceptionHandler {}

or enabling ProbleDetails via the application configuration

spring:
  mvc:
    problemdetails:
      enabled: true

the proper error format (i.e., ProbleDetails) is used for some errors (e.g. 400)

{
    "type": "about:blank",
    "title": "Bad Request",
    "status": 400,
    "detail": "Required parameter 'id' is not present.",
    "instance": "/users"
}

but when an Exception is thrown anywhere inside the code, which results in a 500 error then the default Spring Boot error format is returned instead

{
  "timestamp": "2023-06-02T02:39:40.184+00:00",
  "status": 500,
  "error": "Internal Server Error",
  "path": "/users"
}

this is quite confusing as it means that when enabling the ProblemDetails format, it is not used for every error but only for a subset, and the clients that call the API need to handle 2 different error formats.

A way to fix this in the app is to create a method that handles the extra errors

@ControllerAdvice
public class ErrorHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Throwable.class)
    public ProblemDetail handleExceptions(Throwable exception) {
        return ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    }

}

but this feels like a hack that shouldn't be necessary, as errors should always be returned in the same format from Spring Boot (regardless of the format selected).

I think this would probably be fixed by adding the method shown above inside the ResponseEntityExceptionHandler class, but this would break backward compatibility so I'm unsure if this should be released before the next major version release or if it can indeed be released in the current major version as a bug fix.

If you are happy with the approach I mentioned above I can create a PR for it.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 2, 2023
@quaff
Copy link
Contributor

quaff commented Jun 2, 2023

It's duplication of #33885
I've created #34872 to fix this but declined.

@wilkinsona
Copy link
Member

Thanks, @quaff.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2023
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants