Skip to content

ResponseStatusException.initCause always throws IllegalStateException #27196

@Deroin

Description

@Deroin

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.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions