Skip to content

When using WebFlux, server.error.include-binding-errors=ALWAYS no longer has an effect when the BindingResult exception is the cause of a ResponseStatusException #41984

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

Closed
adamsmith118 opened this issue Aug 21, 2024 · 1 comment
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@adamsmith118
Copy link

Background

Since Boot 2.3 binding errors are no longer included in the default error page by default.

The resolution, until this commit, was to set this property accordingly.

server.error.include-binding-errors=ALWAYS

Unfortunately this no longer works in Boot 3.2.6+.

Expected Behaviour/Repro

Full repro here

Code

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
   System.setProperty("server.error.include-message", "ALWAYS");
   System.setProperty("server.error.include-binding-errors", "ALWAYS");
   SpringApplication.run(DemoApplication.class, args);
}

  @RestController
  public static class Controller {

    @GetMapping("/manualBindError")
    public Mono<ResponseEntity<String>> repro() {
      BindException bindException = new BindException(new RuntimeException("error"), "oh oh");
      ObjectError objectError = new ObjectError("oh oh", new String[] {"CODE"}, null, "MESSAGE");
      bindException.addError(objectError);
      throw new ResponseStatusException(400, "a reason", bindException);
    }
  }
}

When we hit http://localhost:8080/manualBindError

Expected

Something resembling the below - with errors intact.

{
  "timestamp": "2024-08-21T13:20:39.377+00:00",
  "path": "/manualBindError",
  "status": 400,
  "error": "Bad Request",
  "message": "a reason",
  "requestId": "d4b54677-3",
  "errors": [
    {
      "codes": [
        "CODE"
      ],
      "arguments": null,
      "defaultMessage": "MESSAGE",
      "objectName": "oh oh",
      "code": "CODE"
    }
  ]
}

Actual

Errors are missing.

{
  "timestamp": "2024-08-21T13:27:27.504+00:00",
  "path": "/manualBindError",
  "status": 400,
  "error": "Bad Request",
  "requestId": "ae84da6d-1",
  "message": "a reason"
}

Root Cause

The code below was removed from DefaultErrorAttributes.

private Throwable determineException(Throwable error) {
	if (error instanceof ResponseStatusException) {
		return (error.getCause() != null) ? error.getCause() : error;
	}
	return error;
}

This is problematic if the cause was an instance of BindingResult as this is no longer true, so the errors aren't added regardless of property value....

private void handleException(Map<String, Object> errorAttributes, Throwable error,
		MergedAnnotation<ResponseStatus> responseStatusAnnotation, boolean includeStackTrace) {
	Throwable exception;
	if (error instanceof BindingResult bindingResult) {
		errorAttributes.put("message", error.getMessage());
		errorAttributes.put("errors", bindingResult.getAllErrors());
		exception = error;
	}

Resolution?

I can fix this by exposing a custom ErrorAttributes Bean in each app. However, is this the intention or is this a bug? If it's the former then a documentation update would be handy as this is a breaking change.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 21, 2024
@wilkinsona wilkinsona changed the title server.error.include-binding-errors=ALWAYS ignored in Boot 3.2.6+ When using WebFlux, server.error.include-binding-errors=ALWAYS is ignored Aug 21, 2024
@wilkinsona wilkinsona added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 21, 2024
@wilkinsona wilkinsona added this to the 3.2.x milestone Aug 21, 2024
@wilkinsona wilkinsona self-assigned this Aug 21, 2024
@wilkinsona wilkinsona changed the title When using WebFlux, server.error.include-binding-errors=ALWAYS is ignored When using WebFlux, server.error.include-binding-errors=ALWAYS has no effect when the BindingResult exception is the cause of a ResponseStatusException Aug 21, 2024
@wilkinsona wilkinsona changed the title When using WebFlux, server.error.include-binding-errors=ALWAYS has no effect when the BindingResult exception is the cause of a ResponseStatusException When using WebFlux, server.error.include-binding-errors=ALWAYS no longer has an effect when the BindingResult exception is the cause of a ResponseStatusException Aug 21, 2024
@wilkinsona wilkinsona modified the milestones: 3.2.x, 3.2.9 Aug 21, 2024
@adamsmith118
Copy link
Author

Thanks @wilkinsona !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

3 participants