ResponseStatusException.initCause always throws IllegalStateException #27196
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
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 acause
, the third does.For the two constuctors that do not supply a
cause
the third will be called withnull
as value forcause
.This bubbles up to
Throwable
wherecause
will be set to eithernull
or the provided value.When now calling
initCause
on theResponseStatusException
the implementation fromThrowable
will be executed. This one will thow anIllegalStateException
if the member variablecause
doesn't equal theThrowable
instance itself. Since we previously established that it can only benull
of theThrowable
we initially provided it cause this condition will always evaluate tofalse
and thus theIllegalStateException
will 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.The text was updated successfully, but these errors were encountered: