Movatterモバイル変換


[0]ホーム

URL:


Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog InFeature Experimentation
Dev guide
All
Pages
Start typing to search…

Customize the Java SDK error handler

How to create your own error handler logic for the Optimizely Feature Experimentation Java SDK.

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

This error handler is called when an unknown feature flag 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 exceptionsErrorHandler 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 Feature Experimentation Java 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 errorspublic 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;}

Updated 17 days ago



[8]ページ先頭

©2009-2025 Movatter.jp