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 ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Customize error handler for the JavaScript SDK v6

How to create your own error handler logic for the Optimizely Feature Experimentation JavaScript (Browser) SDK.

❗️

Warning

This content covers the Feature Experimentation JavaScript (Browser) SDK v6 features currently in pre-production testing and is subject to change before release

For the latest released version, see JavaScript (Browser) SDK.

Minimum SDK version

v6.0.0+

Description

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

The Optimizely Feature Experimentation SDKs provides an errorNotifier config option to handle such case. To get notified about errors in the SDK, create a errorNotifier using the createErrorNotifier factory function, and pass it to createInstance. createErrorNotifier has a mandatory parameter of theErrorHandlerinterface. With the ErrorHandler, 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.

The following code sample is an example of using the notifier with a custom error handler:

import {
  createInstance,
  createPollingProjectConfigManager,
  createErrorNotifier,
} from "@optimizely/optimizely-sdk";
 
const pollingConfigManager = createPollingProjectConfigManager({
  sdkKey: SDK_KEY,
});

const customErrorHandler = {
  handleError: (error) => {
    console.log('CUSTOM_ERROR_HANDLER');
    console.log('****');
    console.log(`Error Message: ${error.message}`);
    console.log(`Error Stack: ${error.stack}`);
    console.log('****');
  }
}

const errorNotifier = createErrorNotifier(customErrorHandler)

const optimizely = createInstance({
  projectConfigManager: pollingConfigManager,
  errorNotifier
});