Class RestInPeaceHttpException

All Implemented Interfaces:
Serializable

public class RestInPeaceHttpException extends RestInPeaceException
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 Details

    • RestInPeaceHttpException

      public RestInPeaceHttpException(int status, String rawBody, Object errorBody)
      Creates the exception for a failed response with no Retry-After header to parse. Equivalent to RestInPeaceHttpException(status, rawBody, errorBody, null).
      Parameters:
      status - the response's HTTP status
      rawBody - the response's raw body
      errorBody - the response body deserialized into the method's @ErrorType, or rawBody itself 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 status
      rawBody - the response's raw body
      errorBody - the response body deserialized into the method's @ErrorType, or rawBody itself if the method has no @ErrorType
      retryAfterMillis - the response's own Retry-After header (delta-seconds or an HTTP-date), parsed to milliseconds from now, or null if 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 the 4xx range - the request itself was the problem (bad input, missing auth, not found), as opposed to isServerError().
      Returns:
      true for a 4xx status
    • isServerError

      public boolean isServerError()
      Whether the response's status is in the 5xx range - the server itself failed, as opposed to isClientError(). Often the signal to retry (see @Retry's default retryOnStatus) rather than surface the error as the caller's own mistake.
      Returns:
      true for a 5xx status
    • isRedirect

      public boolean isRedirect()
      Whether the response's status is in the 3xx range. 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 (a 304 used for cache revalidation, for instance, is handled internally and never surfaces as this exception at all).
      Returns:
      true for a 3xx status
    • is

      public boolean is(int status)
      Whether the response's status is exactly status - a shorthand for getStatus() == status at a call site (e.g. e.is(404)).

      Ordering pitfall: in an if/else chain, check is(specific status) before isClientError()/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 above
       
       if (e.is(429)) { ... }           // specific case checked first
       else if (e.isClientError()) { ... }   // the rest of 4xx
       
      Parameters:
      status - the status to compare against
      Returns:
      true if this exception's status equals status
    • getRawBody

      public String 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 @ErrorType declared.
      Type Parameters:
      T - the expected error body type
      Returns:
      the error body
    • getRetryAfterMillis

      public Long getRetryAfterMillis()
      Returns the response's own Retry-After header, parsed to milliseconds from now - the exact same parsing @Retry itself uses internally (delta-seconds, a plain integer, or an HTTP-date per RFC 1123), surfaced here for a caller whose method has no @Retry at all (or one that gave up after exhausting its attempts) and wants to honor the server's own backoff hint manually.
      Returns:
      the Retry-After header's value in milliseconds, or null if the header was absent or in neither supported format