Class ClientErrorException

All Implemented Interfaces:
Serializable

public class ClientErrorException extends HttpException
Exception for 4xx HTTP status codes (client errors).

Client errors indicate issues with the request itself:

  • 400 Bad Request - Invalid request format or parameters
  • 401 Unauthorized - Missing or invalid authentication
  • 403 Forbidden - Valid auth but insufficient permissions
  • 404 Not Found - Resource doesn't exist
  • 409 Conflict - Resource state conflict
  • 422 Unprocessable Entity - Validation errors
  • 429 Too Many Requests - Rate limiting (retryable)

Convenience methods for checking specific error types:


 try {
     client.customers().retrieve("invalid_id");
 } catch (ClientErrorException e) {
     if (e.isNotFound()) {
         // Customer doesn't exist
     } else if (e.isUnauthorized()) {
         // Check API key
     } else if (e.isTooManyRequests()) {
         // Back off and retry
         Thread.sleep(e.getRetryAfterMs());
     }
 }
 
See Also:
  • Field Details

    • TOO_MANY_REQUESTS

      public static final int TOO_MANY_REQUESTS
      HTTP status code for rate limiting.
      See Also:
  • Constructor Details

    • ClientErrorException

      public ClientErrorException(int statusCode, String message, Request request, Response response)
      Creates a ClientErrorException.
      Parameters:
      statusCode - the HTTP status code (4xx)
      message - descriptive error message
      request - the original HTTP request
      response - the HTTP response
    • ClientErrorException

      public ClientErrorException(int statusCode, String message, Request request, Response response, Throwable cause)
      Creates a ClientErrorException with cause.
      Parameters:
      statusCode - the HTTP status code (4xx)
      message - descriptive error message
      request - the original HTTP request
      response - the HTTP response
      cause - the underlying cause
  • Method Details

    • isRetryable

      public boolean isRetryable()
      Client errors are generally not retryable, except for 429 Too Many Requests.
      Overrides:
      isRetryable in class HttpException
      Returns:
      true only if status code is 429 (rate limiting)
    • isBadRequest

      public boolean isBadRequest()
      Check if this is a 400 Bad Request error.
      Returns:
      true if status code is 400
    • isUnauthorized

      public boolean isUnauthorized()
      Check if this is a 401 Unauthorized error. Usually indicates missing or invalid API key.
      Returns:
      true if status code is 401
    • isForbidden

      public boolean isForbidden()
      Check if this is a 403 Forbidden error. Usually indicates valid auth but insufficient permissions.
      Returns:
      true if status code is 403
    • isNotFound

      public boolean isNotFound()
      Check if this is a 404 Not Found error.
      Returns:
      true if status code is 404
    • isMethodNotAllowed

      public boolean isMethodNotAllowed()
      Check if this is a 405 Method Not Allowed error.
      Returns:
      true if status code is 405
    • isConflict

      public boolean isConflict()
      Check if this is a 409 Conflict error.
      Returns:
      true if status code is 409
    • isUnprocessableEntity

      public boolean isUnprocessableEntity()
      Check if this is a 422 Unprocessable Entity error. Usually indicates validation errors.
      Returns:
      true if status code is 422
    • isTooManyRequests

      public boolean isTooManyRequests()
      Check if this is a 429 Too Many Requests error. This error is retryable after waiting for the rate limit to reset.
      Returns:
      true if status code is 429
    • getRetryAfterMs

      public long getRetryAfterMs()
      Get the retry-after delay in milliseconds from response headers. This is typically provided with 429 responses.
      Returns:
      the retry delay in milliseconds, or -1 if not available