Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Customize error handler

This topic describes how to create your own error handler logic.

You can provide your own custom error handler logic to standardize across your production environment.

This error handler is called when an unknown feature key is referenced.

Here is a code example where an error handler from the SDK is used. If the error handler is not overridden, a no-op error handler is used by default.

import com.optimizely.ab.Optimizely;
import com.optimizely.ab.error.ErrorHandler;
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
import com.optimizely.ab.event.AsyncEventHandler;
import com.optimizely.ab.event.EventHandler;

EventHandler eventHandler = new AsyncEventHandler(20000, 1);

// Default handler that raises exceptions
ErrorHandler errorHandler = new RaiseExceptionErrorHandler();

Optimizely optimizelyClient;
try {
    // Create an Optimizely client with the default event dispatcher
    optimizelyClient = Optimizely.builder(datafile, eventHandler)
                           .withErrorHandler(errorHandler)
                           .build();
} catch (ConfigParseException e) {
    // Handle exception gracefully
    return;
}

For additional control and visibility into the errors coming from the Optimizely SDK, we recommend implementing your own custom error handler. With a custom error handler, you can choose what to do with an error, whether it may be as simple as logging the error to console or sending the error to another error monitoring service. Below is an example of using a custom error handler to log errors:

import com.optimizely.ab.Optimizely;
import com.optimizely.ab.error.ErrorHandler;
import com.optimizely.ab.OptimizelyRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Custom handler that logs the errors
public class CustomErrorHandler implements ErrorHandler {
  	private static final Logger logger = LoggerFactory.getLogger(CustomErrorHandler.class);
  
    @Override
    public <T extends OptimizelyRuntimeException> void handleError(T exception) {
        logger.error(exception.getMessage());
    }
}

EventHandler eventHandler = new AsyncEventHandler(20000, 1);
ErrorHandler customErrorHandler = new CustomErrorHandler();

Optimizely optimizelyClient;
try {
    // Create an Optimizely client with the default event dispatcher
    optimizelyClient = Optimizely.builder(datafile, eventHandler)
                           .withErrorHandler(customErrorHandler)
                           .build();
} catch (ConfigParseException e) {
    // Handle exception gracefully
    return;
}