Client configuration APIs and shared utilities for Netflix's Ribbon IPC library, providing core interfaces for load balancing, fault tolerance, and service discovery capabilities in cloud environments.
—
Comprehensive exception hierarchy with error codes, types, and detailed error information for proper error handling in distributed client applications. The exception system provides structured error reporting and categorization.
General exception class for client operations with error code support and nested error types.
/**
* General exception class for client operations with error code support and nested error types
*/
public class ClientException extends Exception {
/**
* Serial version UID for serialization
*/
public static final long serialVersionUID = -7697654244064441234L;
/**
* Creates exception with message
* @param message the error message
*/
public ClientException(String message);
/**
* Creates exception with error code
* @param errorCode the numeric error code
*/
public ClientException(int errorCode);
/**
* Creates exception with error code and message
* @param errorCode the numeric error code
* @param message the error message
*/
public ClientException(int errorCode, String message);
/**
* Creates exception with chained exception
* @param chainedException the cause of this exception
*/
public ClientException(Throwable chainedException);
/**
* Creates exception with message and chained exception
* @param message the error message
* @param chainedException the cause of this exception
*/
public ClientException(String message, Throwable chainedException);
/**
* Creates exception with error code, message, and chained exception
* @param errorCode the numeric error code
* @param message the error message
* @param chainedException the cause of this exception
*/
public ClientException(int errorCode, String message, Throwable chainedException);
/**
* Creates exception with error type
* @param error the error type
*/
public ClientException(ErrorType error);
/**
* Creates exception with error type and message
* @param error the error type
* @param message the error message
*/
public ClientException(ErrorType error, String message);
/**
* Creates exception with error type, message, and chained exception
* @param error the error type
* @param message the error message
* @param chainedException the cause of this exception
*/
public ClientException(ErrorType error, String message, Throwable chainedException);
/**
* Returns the error type
* @return the ErrorType associated with this exception
*/
public ErrorType getErrorType();
/**
* Returns the error code
* @return the numeric error code
*/
public int getErrorCode();
/**
* Sets the error code
* @param errorCode the numeric error code to set
*/
public void setErrorCode(int errorCode);
/**
* Returns the error message
* @return the error message
*/
public String getErrorMessage();
/**
* Sets the error message
* @param msg the error message to set
*/
public void setErrorMessage(String msg);
/**
* Returns the error object
* @return additional error context object
*/
public Object getErrorObject();
/**
* Sets the error object
* @param errorObject additional error context object
*/
public void setErrorObject(Object errorObject);
/**
* Returns internal message for the exception
* @return internal diagnostic message
*/
public String getInternalMessage();
/**
* Returns error codes mapping for a class
* @param clazz the class to get error codes for
* @return HashMap mapping error names to codes
*/
public static HashMap getErrorCodes(Class clazz);
/**
* Error type enumeration categorizing different types of client errors
*/
public enum ErrorType {
/** General unspecified error */
GENERAL,
/** Configuration-related error */
CONFIGURATION,
/** Maximum retries on same server exceeded */
NUMBEROF_RETRIES_EXEEDED,
/** Maximum retries across servers exceeded */
NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED,
/** Socket timeout during operation */
SOCKET_TIMEOUT_EXCEPTION,
/** Read timeout during operation */
READ_TIMEOUT_EXCEPTION,
/** Unknown host error */
UNKNOWN_HOST_EXCEPTION,
/** Connection establishment error */
CONNECT_EXCEPTION,
/** Client-side throttling applied */
CLIENT_THROTTLED,
/** Server-side throttling detected */
SERVER_THROTTLED,
/** No network route to host */
NO_ROUTE_TO_HOST_EXCEPTION,
/** Required data missing from cache */
CACHE_MISSING
}
}Usage Examples:
import com.netflix.client.ClientException;
import com.netflix.client.ClientException.ErrorType;
// Basic exception with message
throw new ClientException("Failed to connect to service");
// Exception with error type
throw new ClientException(ErrorType.CONNECT_EXCEPTION, "Could not establish connection");
// Exception with error code and chained cause
try {
// Some network operation
} catch (SocketTimeoutException e) {
throw new ClientException(ErrorType.SOCKET_TIMEOUT_EXCEPTION,
"Request timed out after 5 seconds", e);
}
// Exception handling with type checking
try {
// Client operation
} catch (ClientException e) {
switch (e.getErrorType()) {
case SOCKET_TIMEOUT_EXCEPTION:
// Handle timeout specifically
logger.warn("Request timed out, will retry");
break;
case CONNECT_EXCEPTION:
// Handle connection issues
logger.error("Connection failed: " + e.getErrorMessage());
break;
case CLIENT_THROTTLED:
// Handle throttling
logger.info("Request was throttled, backing off");
Thread.sleep(1000);
break;
default:
logger.error("Unexpected client error", e);
}
}Exception thrown when HTTP response is unexpected.
/**
* Exception thrown when HTTP response is unexpected
*/
public class UnexpectedHttpResponseException extends Exception {
/**
* Serial version UID for serialization
*/
public static final long serialVersionUID = 1L;
/**
* Creates exception with HTTP status information
* @param statusCode the HTTP status code
* @param statusLine the HTTP status line
*/
public UnexpectedHttpResponseException(int statusCode, String statusLine);
/**
* Returns HTTP status code
* @return the HTTP status code that caused this exception
*/
public int getStatusCode();
/**
* Returns HTTP status line
* @return the HTTP status line that caused this exception
*/
public String getStatusLine();
}Usage Examples:
import com.netflix.client.http.UnexpectedHttpResponseException;
// In HTTP client implementation
public void handleHttpResponse(int statusCode, String statusLine) throws UnexpectedHttpResponseException {
if (statusCode >= 400) {
throw new UnexpectedHttpResponseException(statusCode, statusLine);
}
}
// Exception handling
try {
// HTTP request
} catch (UnexpectedHttpResponseException e) {
int status = e.getStatusCode();
String line = e.getStatusLine();
if (status >= 500) {
// Server error - might be retriable
logger.warn("Server error: {} - {}", status, line);
} else if (status >= 400) {
// Client error - typically not retriable
logger.error("Client error: {} - {}", status, line);
}
}Exception for SSL socket factory problems.
/**
* Exception for SSL socket factory problems
*/
public class ClientSslSocketFactoryException extends Exception {
/**
* Serial version UID for serialization
*/
public static final long serialVersionUID = 1L;
/**
* Creates SSL exception with message and cause
* @param message the error message
* @param cause the underlying cause
*/
public ClientSslSocketFactoryException(String message, Throwable cause);
}Usage Examples:
import com.netflix.client.ssl.ClientSslSocketFactoryException;
// In SSL context factory
public SSLContext createSSLContext() throws ClientSslSocketFactoryException {
try {
// SSL context creation logic
return sslContext;
} catch (KeyStoreException e) {
throw new ClientSslSocketFactoryException("Failed to load keystore", e);
} catch (NoSuchAlgorithmException e) {
throw new ClientSslSocketFactoryException("SSL algorithm not available", e);
}
}
// Exception handling
try {
SSLContext context = factory.createSSLContext();
} catch (ClientSslSocketFactoryException e) {
logger.error("SSL configuration error: " + e.getMessage(), e.getCause());
// Fall back to default SSL configuration
}/**
* Comprehensive exception handling for client operations
*/
public class RobustClient {
private static final Logger logger = LoggerFactory.getLogger(RobustClient.class);
public void executeRequest(ClientRequest request) {
try {
// Execute client request
performRequest(request);
} catch (ClientException e) {
handleClientException(e);
} catch (UnexpectedHttpResponseException e) {
handleHttpException(e);
} catch (ClientSslSocketFactoryException e) {
handleSslException(e);
} catch (Exception e) {
handleGenericException(e);
}
}
private void handleClientException(ClientException e) {
ErrorType errorType = e.getErrorType();
int errorCode = e.getErrorCode();
String message = e.getErrorMessage();
// Log with appropriate level based on error type
switch (errorType) {
case SOCKET_TIMEOUT_EXCEPTION:
case READ_TIMEOUT_EXCEPTION:
logger.warn("Timeout error ({}): {}", errorCode, message);
// Might be retriable
break;
case CONNECT_EXCEPTION:
case NO_ROUTE_TO_HOST_EXCEPTION:
logger.error("Connection error ({}): {}", errorCode, message);
// Check if service is down
break;
case CLIENT_THROTTLED:
case SERVER_THROTTLED:
logger.info("Throttling detected ({}): {}", errorCode, message);
// Implement backoff strategy
break;
case CONFIGURATION:
logger.error("Configuration error ({}): {}", errorCode, message);
// Fix configuration
break;
default:
logger.error("Client error ({}): {}", errorCode, message, e);
}
// Additional error context
Object errorObject = e.getErrorObject();
if (errorObject != null) {
logger.debug("Error context: {}", errorObject);
}
}
private void handleHttpException(UnexpectedHttpResponseException e) {
int statusCode = e.getStatusCode();
String statusLine = e.getStatusLine();
if (statusCode >= 500) {
logger.warn("Server error: {} {}", statusCode, statusLine);
// Server error - might retry with backoff
} else if (statusCode >= 400) {
logger.error("Client error: {} {}", statusCode, statusLine);
// Client error - fix request before retry
}
}
private void handleSslException(ClientSslSocketFactoryException e) {
logger.error("SSL configuration error: {}", e.getMessage(), e);
// Check SSL certificates and configuration
}
private void handleGenericException(Exception e) {
logger.error("Unexpected error during request execution", e);
// Generic error handling
}
}/**
* Example of error recovery with exponential backoff
*/
public class RetryableClient {
private static final int MAX_RETRIES = 3;
private static final long BASE_DELAY_MS = 1000;
public void executeWithRecovery(ClientRequest request) throws ClientException {
int attempt = 0;
while (attempt <= MAX_RETRIES) {
try {
performRequest(request);
return; // Success
} catch (ClientException e) {
if (!isRetriable(e) || attempt == MAX_RETRIES) {
throw e; // Don't retry or max attempts reached
}
attempt++;
long delay = calculateBackoffDelay(attempt);
logger.warn("Request failed (attempt {}), retrying in {}ms: {}",
attempt, delay, e.getErrorMessage());
try {
Thread.sleep(delay);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new ClientException("Request interrupted during retry", ie);
}
}
}
}
private boolean isRetriable(ClientException e) {
switch (e.getErrorType()) {
case SOCKET_TIMEOUT_EXCEPTION:
case READ_TIMEOUT_EXCEPTION:
case CONNECT_EXCEPTION:
return true;
case CLIENT_THROTTLED:
case SERVER_THROTTLED:
return true;
default:
return false;
}
}
private long calculateBackoffDelay(int attempt) {
return BASE_DELAY_MS * (1L << (attempt - 1)); // Exponential backoff
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-netflix-ribbon--ribbon-core