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

Incorrect AsyncRequestTimeoutException handling in ResponseEntityExceptionHandler #32644

Open
lexakimov opened this issue Apr 16, 2024 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@lexakimov
Copy link

Affects: 6.1.5

Hello. I noticed that when using the RFC7807 error handler, the AsyncRequestTimeoutException is not handled correctly.

The org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler class handles this exception:

else if (ex instanceof AsyncRequestTimeoutException subEx) {
    return handleAsyncRequestTimeoutException(subEx, subEx.getHeaders(), subEx.getStatusCode(), request);
}

however, the getBody() method in the exception itself always returns a new object:

@Override
public ProblemDetail getBody() {
    return ProblemDetail.forStatus(getStatusCode());
}

Because of this, populating the fields in the org.springframework.web.ErrorResponse#updateAndGetBody method (that called from org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleExceptionInternal) has no effect.

Reproduction:

  • enable a rfc7807 handler: spring.mvc.problemdetails.enabled: true.
  • add custom messages to default message source
# AsyncRequestTimeoutException
problemDetail.type.org.springframework.web.context.request.async.AsyncRequestTimeoutException=about:blank
problemDetail.title.org.springframework.web.context.request.async.AsyncRequestTimeoutException=request timeout
problemDetail.org.springframework.web.context.request.async.AsyncRequestTimeoutException=timeout reached
  • create controller and throw exception like this:
   @GetMapping("/springError/AsyncRequestTimeoutException")
    public TestResponseDto asyncRequestTimeoutException() {
        throw new AsyncRequestTimeoutException();
    }

we expect to see a response like this:

{
    "type": "about:blank",
    "title": "request timeout",
    "status": 500,
    "detail": "timeout reached",
    "instance": "/api/test/springError/AsyncRequestTimeoutException"
}

but in fact the response is:

{
    "type": "about:blank",
    "title": "Service Unavailable",
    "status": 503,
    "instance": "/api/test/springError/AsyncRequestTimeoutException"
}

Suggestion

I guess it can be fixed in AsyncRequestTimeoutException like that:

	private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());
        ...
	@Override
	public ProblemDetail getBody() {
		return this.body;
	}

Workaround
as a workaround, i overridden method ru.wbbank.error.handler.starter.webmvc.controller.WBProblemDetailsExceptionHandler#handleAsyncRequestTimeoutException.

We create problemDetail manually, not by AsyncRequestTimeoutException:

    @Override
    protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
        AsyncRequestTimeoutException ex,
        HttpHeaders headers,
        HttpStatusCode status,
        WebRequest request
    ) {
        var problemDetail = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
        var errResp = ErrorResponse.builder(ex, problemDetail).build();
        problemDetail = errResp.updateAndGetBody(getMessageSource(), LocaleContextHolder.getLocale());
        return this.handleExceptionInternal(ex, problemDetail, errResp.getHeaders(), errResp.getStatusCode(), request);
    }
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 16, 2024
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 16, 2024
@rstoyanchev rstoyanchev self-assigned this Apr 16, 2024
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 16, 2024
@rstoyanchev rstoyanchev added this to the 6.1.7 milestone Apr 16, 2024
@facewise
Copy link

@rstoyanchev
Can I pick up this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants