Class ApiException

  • All Implemented Interfaces:
    Serializable

    public class ApiException
    extends RocketRestException
    Exception thrown when an API request fails with rich error details.

    This exception extends RocketRestException and provides additional context about API failures including the error message from the server. Use this when you need more details than the base exception provides.

    Exception Hierarchy

     RuntimeException
       └── RocketRestException (base HTTP exception)
             ├── CircuitBreakerOpenException
             ├── TokenExpiredException
             └── ApiException (richer error details)
                   └── errorMessage: Server error message
     

    Handling ApiException

    
     try {
         User user = client.get("/users/999", User.class);
     } catch (ApiException e) {
         System.err.println("Status: " + e.getStatusCode());
         System.err.println("Message: " + e.getErrorMessage());
         System.err.println("Body: " + e.getResponseBody());
     } catch (RocketRestException e) {
         // Handles all other HTTP exceptions
         System.err.println("HTTP error: " + e.getStatusCode());
     }
     

    Avoiding Exceptions with Result Pattern

    Consider using the fluent API with Result to avoid exceptions:

    
     Result<User, ApiError> result = client.fluent().get("/users/999", User.class);
     result.match(
         user -> handleSuccess(user),
         error -> handleError(error)  // No exception thrown
     );
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    RocketRestException, Result, ApiError, Serialized Form
    • Constructor Detail

      • ApiException

        public ApiException​(String message,
                            String responseBody,
                            String errorMessage,
                            int statusCode)
        Creates a new ApiException with full error details.
        Parameters:
        message - The exception message
        responseBody - The raw response body from the server
        errorMessage - The parsed error message from the server
        statusCode - The HTTP status code
      • ApiException

        public ApiException​(String message)
        Creates a new ApiException with just a message.
        Parameters:
        message - The exception message
      • ApiException

        public ApiException​(String message,
                            Throwable cause)
        Creates a new ApiException with a message and cause.
        Parameters:
        message - The exception message
        cause - The underlying cause
    • Method Detail

      • getErrorMessage

        public String getErrorMessage()
        Gets the parsed error message from the server response. This is often a more user-friendly message extracted from the response body.
        Returns:
        The server error message, or null if not available