Package com.shri.restinpeace.exception
Class RestInPeaceHttpException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.shri.restinpeace.exception.RestInPeaceException
com.shri.restinpeace.exception.RestInPeaceHttpException
- All Implemented Interfaces:
Serializable
Unchecked exception thrown when a
@RestClient method's response
has a non-2xx HTTP status - the request reached the server and got a
response, it's just not a success. Carries the status and the raw
response body, plus the body deserialized into whatever class a
@ErrorType on the
method declared (or the raw body itself, as a String, if the
method has no @ErrorType).
A transport failure (no response at all, e.g. connection refused) still throws the underlying transport exception directly, not this one - this exception specifically means "the server answered, and the answer was an error".
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRestInPeaceHttpException(int status, String rawBody, Object errorBody) Creates the exception for a failed response with noRetry-Afterheader to parse.RestInPeaceHttpException(int status, String rawBody, Object errorBody, Long retryAfterMillis) Creates the exception for a failed response. -
Method Summary
Modifier and TypeMethodDescription<T> TReturns the response body deserialized into the method's@ErrorType, or the raw body itself if the method has no@ErrorType.Returns the response's raw body, regardless of whether the method declared an@ErrorType.Returns the response's ownRetry-Afterheader, parsed to milliseconds from now - the exact same parsing@Retryitself uses internally (delta-seconds, a plain integer, or an HTTP-date per RFC 1123), surfaced here for a caller whose method has no@Retryat all (or one that gave up after exhausting its attempts) and wants to honor the server's own backoff hint manually.intReturns the response's HTTP status.booleanis(int status) Whether the response's status is exactlystatus- a shorthand forgetStatus() == statusat a call site (e.g.booleanWhether the response's status is in the4xxrange - the request itself was the problem (bad input, missing auth, not found), as opposed toisServerError().booleanWhether the response's status is in the3xxrange.booleanWhether the response's status is in the5xxrange - the server itself failed, as opposed toisClientError().Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
RestInPeaceHttpException
Creates the exception for a failed response with noRetry-Afterheader to parse. Equivalent toRestInPeaceHttpException(status, rawBody, errorBody, null).- Parameters:
status- the response's HTTP statusrawBody- the response's raw bodyerrorBody- the response body deserialized into the method's@ErrorType, orrawBodyitself if the method has no@ErrorType
-
RestInPeaceHttpException
public RestInPeaceHttpException(int status, String rawBody, Object errorBody, Long retryAfterMillis) Creates the exception for a failed response.- Parameters:
status- the response's HTTP statusrawBody- the response's raw bodyerrorBody- the response body deserialized into the method's@ErrorType, orrawBodyitself if the method has no@ErrorTyperetryAfterMillis- the response's ownRetry-Afterheader (delta-seconds or an HTTP-date), parsed to milliseconds from now, ornullif the header was absent or unparseable
-
-
Method Details
-
getStatus
public int getStatus()Returns the response's HTTP status.- Returns:
- the HTTP status
-
isClientError
public boolean isClientError()Whether the response's status is in the4xxrange - the request itself was the problem (bad input, missing auth, not found), as opposed toisServerError().- Returns:
truefor a4xxstatus
-
isServerError
public boolean isServerError()Whether the response's status is in the5xxrange - the server itself failed, as opposed toisClientError(). Often the signal to retry (see@Retry's defaultretryOnStatus) rather than surface the error as the caller's own mistake.- Returns:
truefor a5xxstatus
-
isRedirect
public boolean isRedirect()Whether the response's status is in the3xxrange. A redirect response is typically followed transparently by the underlying HTTP client before RIP ever sees a final status, so this is mostly relevant with redirect-following disabled at that layer, or for a status this library doesn't otherwise treat specially (a304used for cache revalidation, for instance, is handled internally and never surfaces as this exception at all).- Returns:
truefor a3xxstatus
-
is
public boolean is(int status) Whether the response's status is exactlystatus- a shorthand forgetStatus() == statusat a call site (e.g.e.is(404)).Ordering pitfall: in an if/else chain, check
is(specific status)beforeisClientError()/isServerError(), not after - since every status those two cover already falls in their range, a branch for one specific status placed after either check is unreachable dead code:if (e.isClientError()) { ... } // matches 429 too - reached first else if (e.is(429)) { ... } // dead code: 429 already handled aboveif (e.is(429)) { ... } // specific case checked first else if (e.isClientError()) { ... } // the rest of 4xx- Parameters:
status- the status to compare against- Returns:
trueif this exception's status equalsstatus
-
getRawBody
Returns the response's raw body, regardless of whether the method declared an@ErrorType.- Returns:
- the raw response body
-
getErrorBody
public <T> T getErrorBody()Returns the response body deserialized into the method's@ErrorType, or the raw body itself if the method has no@ErrorType. The caller is responsible for requesting the same type the method's@ErrorTypedeclared.- Type Parameters:
T- the expected error body type- Returns:
- the error body
-
getRetryAfterMillis
Returns the response's ownRetry-Afterheader, parsed to milliseconds from now - the exact same parsing@Retryitself uses internally (delta-seconds, a plain integer, or an HTTP-date per RFC 1123), surfaced here for a caller whose method has no@Retryat all (or one that gave up after exhausting its attempts) and wants to honor the server's own backoff hint manually.- Returns:
- the
Retry-Afterheader's value in milliseconds, ornullif the header was absent or in neither supported format
-