Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Customize the PHP SDK error handler

How to create your own error handler logic.

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.

use Optimizely\ErrorHandler\DefaultErrorHandler;

$optimizelyClient = new Optimizely($datafile, null, null, new DefaultErrorHandler());

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 SDK.

use Optimizely\ErrorHandler\ErrorHandlerInterface;

/**
 * Class MyCustomErrorHandler
 * 
 * Custom error handler class that implements the ErrorHandlerInterface
 * allowing you to define custom behavior.
 */
class MyCustomErrorHandler implements ErrorHandlerInterface
{
    /**
     * handleError
     *
     * Method which gets called when an error is thrown in the SDK
     * @param Exception $error - the exception object
     */
    public function handleError(Exception $error)
    {
        // You can use custom logic here to handle the error as needed.
        // For example, echo the raw exception message and stacktrace:
        echo "CUSTOM_ERROR_HANDLER\n";
        echo "****\n";
        echo "Error Message: " . $error->getMessage() . "\n";
        echo "Error Stack: " . $error->getTraceAsString() . "\n";
        echo "****\n";
        // You you may also wish to throw the exception 
        // throw $error;
        // or handle it in another way per your requirements.
    }
}

// This error handler can then be used when initializing the Optimizely SDK.
$optimizelyClient = new Optimizely($datafile, null, null, new MyCustomErrorHandler());