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

ResponseStatusException.initCause always throws IllegalStateException #27196

Closed
Deroin opened this issue Jul 22, 2021 · 0 comments
Closed

ResponseStatusException.initCause always throws IllegalStateException #27196

Deroin opened this issue Jul 22, 2021 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@Deroin
Copy link

Deroin commented Jul 22, 2021

Affected versions: From at least 5.2.1.RELEASE to current master (probably even older versions)

Scenario:
We use ResponseStatusException to cause Spring to return certain status codes in certain scenarios. When instantiating such Exceptions we can use one of three constructors. Two do not supply a cause, the third does.
For the two constuctors that do not supply a cause the third will be called with null as value for cause.

This bubbles up to Throwable where cause will be set to either null or the provided value.

When now calling initCause on the ResponseStatusException the implementation from Throwable will be executed. This one will thow an IllegalStateException if the member variable cause doesn't equal the Throwable instance itself. Since we previously established that it can only be null of the Throwable we initially provided it cause this condition will always evaluate to false and thus the IllegalStateException will always be thrown.

Code example:

Exception someException = new Exception();
ResponseStatusException rse = new ResponseStatusException(HttpStatus.BAD_REQUEST);
rse.initCause(someException);
throw rse;

We know that this can be worked around with calling new ResponseStatusException(status, null, someException), still it is bad that there is a method that will always cause unexpected behavior.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 22, 2021
@jhoeller jhoeller self-assigned this Jul 23, 2021
@jhoeller jhoeller added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 23, 2021
@jhoeller jhoeller added this to the 5.3.10 milestone Jul 23, 2021
@jhoeller jhoeller added the for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x label Jul 26, 2021
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x labels Jul 26, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
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) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants