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 the React SDK error handler

This topic describes how to create your own error handler logic for the Optimizely Feature Experimentation React SDK.

In a production environment, you will want to have full control and visibility over the errors that are happening in your application, including those that would originate from an Optimizely Feature Experimentation SDK.

The Optimizely Feature Experimentation SDKs provide default implementations of an error handler in the SDKs, which is a no-op handler. Below is an example of using the default error handler from the SDKs:

import {
  errorHandler,
  createInstance,
} from '@optimizely/react-sdk'

const optimizely = createInstance({
  // No datafile will trigger the custom error handler,
  // but the default error handler is a no-op
  datafile: null,
  errorHandler: errorHandler
});

However, for additional control and visibility into the errors coming from the Optimizely Feature Experimentation 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 to the console:

import {
  createInstance,
} from '@optimizely/react-sdk'

/**
 * customErrorHandler
 *
 * Object that has a property `handleError` which will be called
 * when an error is thrown in the SDK.
 */
const customErrorHandler = {
  /**
   * handleError
   *
   * Function which gets called when an error is thrown in the SDK
   * @param {Object} error - error object
   * @param {String} error.message - message of the error
   * @param {String} error.stack - stack trace for the error
   */
  handleError: function(error) {
    console.log('CUSTOM_ERROR_HANDLER');
    console.log('****');
    console.log(`Error Message: ${error.message}`);
    console.log(`Error Stack: ${error.stack}`);
    console.log('****');
  }
}
 
const optimizelyClientInstance = OptimizelySdk.createInstance({
  // No datafile will trigger the custom error handler,
  datafile: null,
  errorHandler: customErrorHandler,
});

🚧

Important

Turning off logging with setLogger(null) will suppress the error handler from handling an error.