Class ApiError


  • public class ApiError
    extends Object
    Represents an API error in the Result pattern.

    This class encapsulates information about API errors without using exceptions, providing a functional approach to error handling. It includes the error message, HTTP status code (when applicable), response body, and error type classification.

    Error Types

    Creating Errors

    
     // HTTP error with status code
     ApiError notFound = ApiError.httpError("User not found", 404, responseBody);
    
     // Network error
     ApiError networkError = ApiError.networkError("Connection refused");
    
     // Parse error
     ApiError parseError = ApiError.parseError("Invalid JSON", responseBody);
     

    Handling Errors

    
     Result<User, ApiError> result = client.fluent().get("/users/1", User.class);
    
     result.ifFailure(error -> {
         if (error.isType(ErrorType.HTTP_ERROR)) {
             if (error.hasStatusCode(404)) {
                 System.out.println("User not found");
             } else if (error.hasStatusCode(500)) {
                 System.out.println("Server error");
             }
         } else if (error.isType(ErrorType.NETWORK_ERROR)) {
             System.out.println("Check your connection");
         }
     });
     
    Since:
    1.0.0
    Author:
    guinetik <guinetik@gmail.com>
    See Also:
    Result, FluentApiClient
    • Constructor Detail

      • ApiError

        public ApiError​(String message,
                        int statusCode,
                        String responseBody,
                        ApiError.ErrorType errorType)
        Constructs a new ApiError with the specified parameters.
        Parameters:
        message - Error message
        statusCode - HTTP status code (may be 0 for non-HTTP errors)
        responseBody - Response body or null if not available
        errorType - Type of error that occurred
    • Method Detail

      • httpError

        public static ApiError httpError​(String message,
                                         int statusCode,
                                         String responseBody)
        Constructs an HTTP error with status code.
        Parameters:
        message - Error message
        statusCode - HTTP status code
        responseBody - Response body
        Returns:
        A new ApiError representing an HTTP error
      • networkError

        public static ApiError networkError​(String message)
        Constructs a network error.
        Parameters:
        message - Error message
        Returns:
        A new ApiError representing a network error
      • parseError

        public static ApiError parseError​(String message,
                                          String responseBody)
        Constructs a parse error.
        Parameters:
        message - Error message
        responseBody - The response that couldn't be parsed
        Returns:
        A new ApiError representing a parse error
      • authError

        public static ApiError authError​(String message,
                                         int statusCode,
                                         String responseBody)
        Constructs an authentication error.
        Parameters:
        message - Error message
        statusCode - HTTP status code (typically 401)
        responseBody - Response body
        Returns:
        A new ApiError representing an authentication error
      • configError

        public static ApiError configError​(String message)
        Constructs a configuration error.
        Parameters:
        message - Error message
        Returns:
        A new ApiError representing a configuration error
      • circuitOpenError

        public static ApiError circuitOpenError​(String message)
        Constructs a circuit breaker open error.
        Parameters:
        message - Error message
        Returns:
        A new ApiError representing a circuit breaker open error
      • getMessage

        public String getMessage()
        Gets the error message.
        Returns:
        The error message
      • getStatusCode

        public int getStatusCode()
        Gets the HTTP status code.
        Returns:
        The HTTP status code, or 0 if not applicable
      • getResponseBody

        public String getResponseBody()
        Gets the response body.
        Returns:
        The response body, or null if not available
      • getErrorType

        public ApiError.ErrorType getErrorType()
        Gets the error type.
        Returns:
        The error type
      • isType

        public boolean isType​(ApiError.ErrorType type)
        Checks if this error is of the specified type.
        Parameters:
        type - The error type to check
        Returns:
        true if this error is of the specified type, false otherwise
      • hasStatusCode

        public boolean hasStatusCode​(int statusCode)
        Checks if this error is an HTTP error with the specified status code.
        Parameters:
        statusCode - The HTTP status code to check
        Returns:
        true if this error is an HTTP error with the specified status code