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 SDK versions 6 and above.

❗️

Warning

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

Minimum SDK version

v6.0.0+

For versions 5.3.5 and below, see JavaScript (Browser) SDK or JavaScript (Node) SDK. See the SDK compatibility matrix documentation for a list of current SDK releases and the features they support.

Description

In production, it is important to monitor and manage errors from all parts of your application, including those from the Optimizely Feature Experimentation JavaScript SDK.

The SDK provides an errorNotifier configuration option that lets you track and respond to these errors. To use it, create an error notifier using the createErrorNotifier factory function and pass it to createInstance.

The createErrorNotifier function requires an ErrorHandler, which defines how to handle each error. You can use this handler to log errors to the console, forward them to an external monitoring service, or take any other appropriate action.

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
});