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

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Customize Python SDK error handler

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

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

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

See the code example below. If the error handler is not overridden, a no-op error handler is used by default.

from optimizely.error_handler import NoOpErrorHandler as error_handler
from optimizely import optimizely

optimizely_client = optimizely.Optimizely(datafile,
                                          event_dispatcher=event_dispatcher,
                                          logger=logger,
                                          error_handler=error_handler)

To have finer-grained control over your SDK configuration in a production environment, you can also pass in a custom error handler for your Optimizely client. A custom error handler can allow you to have more control over how you want to handle any errors coming from the Optimizely Feature Experimentation Python SDK.

from optimizely import optimizely
from optimizely import error_handler


class MyCustomErrorHandler(error_handler.BaseHandler):
  """ Custom error handler class that extends and overrides the 
      handle_error method allowing you to define custom behavior. 
  """
  
  @staticmethod
  def handle_error(error):
    # You can put custom logic here about how you want to handle the error.
    # For example here we simply raise the error.
    raise error
    

# Here we initialize the SDK with the custom error handler.
optimizely_client = optimizely.Optimizely(datafile, error_handler=MyCustomErrorHandler)