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.
Affected versions: From at least 5.2.1.RELEASE to current master (probably even older versions)
Scenario:
We use
ResponseStatusExceptionto 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 acause, the third does.For the two constuctors that do not supply a
causethe third will be called withnullas value forcause.This bubbles up to
Throwablewherecausewill be set to eithernullor the provided value.When now calling
initCauseon theResponseStatusExceptionthe implementation fromThrowablewill be executed. This one will thow anIllegalStateExceptionif the member variablecausedoesn't equal theThrowableinstance itself. Since we previously established that it can only benullof theThrowablewe initially provided it cause this condition will always evaluate tofalseand thus theIllegalStateExceptionwill always be thrown.Code example:
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.