Package com.guinetik.rr.result
Class ApiError
- java.lang.Object
-
- com.guinetik.rr.result.ApiError
-
public class ApiError extends Object
Represents an API error in theResultpattern.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
ApiError.ErrorType.HTTP_ERROR- Server returned an error status codeApiError.ErrorType.NETWORK_ERROR- Connection or network failureApiError.ErrorType.PARSE_ERROR- Failed to parse response bodyApiError.ErrorType.AUTH_ERROR- Authentication/authorization failureApiError.ErrorType.CONFIG_ERROR- Client misconfigurationApiError.ErrorType.CIRCUIT_OPEN- Circuit breaker is open
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classApiError.ErrorTypeEnum representing different types of errors that can occur.
-
Constructor Summary
Constructors Constructor Description ApiError(String message, int statusCode, String responseBody, ApiError.ErrorType errorType)Constructs a new ApiError with the specified parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ApiErrorauthError(String message, int statusCode, String responseBody)Constructs an authentication error.static ApiErrorcircuitOpenError(String message)Constructs a circuit breaker open error.static ApiErrorconfigError(String message)Constructs a configuration error.ApiError.ErrorTypegetErrorType()Gets the error type.StringgetMessage()Gets the error message.StringgetResponseBody()Gets the response body.intgetStatusCode()Gets the HTTP status code.booleanhasStatusCode(int statusCode)Checks if this error is an HTTP error with the specified status code.static ApiErrorhttpError(String message, int statusCode, String responseBody)Constructs an HTTP error with status code.booleanisType(ApiError.ErrorType type)Checks if this error is of the specified type.static ApiErrornetworkError(String message)Constructs a network error.static ApiErrorparseError(String message, String responseBody)Constructs a parse error.StringtoString()
-
-
-
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 messagestatusCode- HTTP status code (may be 0 for non-HTTP errors)responseBody- Response body or null if not availableerrorType- 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 messagestatusCode- HTTP status coderesponseBody- 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 messageresponseBody- 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 messagestatusCode- 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
-
-