PHP code example of yii1tech / error-handler

1. Go to this page and download the library: Download yii1tech/error-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yii1tech / error-handler example snippets




return [
    'preload' => [
        'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion
        // ...
    ],
    'components' => [
        'errorHandler' => [
            'class' => \yii1tech\error\handler\ErrorHandler::class,
        ],
        // ...
    ],
    // ...
];



try {
    // ...
    trigger_error('Some custom error message', E_USER_WARNING);
    // ...
} catch (ErrorException $exception) {
    // handle error
}



return [
    'preload' => [
        'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion
        // ...
    ],
    'components' => [
        'errorHandler' => [
            'class' => \yii1tech\error\handler\ErrorHandler::class,
            'shouldRenderErrorAsJsonCallback' => function () {
                if (!empty($_SERVER['HTTP_ACCEPT']) && strcasecmp($_SERVER['HTTP_ACCEPT'], 'application/json') === 0) {
                    return true;
                }
                
                if (Yii::app()->request->isAjaxRequest) {
                    return true;
                }
                
                if (str_starts_with(Yii::app()->request->getPathInfo(), 'api/')) {
                    return true;
                }
                
                return false;
            },
        ],
        // ...
    ],
    // ...
];



class SiteController extends CController
{
    public function actionError(): void
    {
        /** @var \yii1tech\error\handler\ErrorHandler $errorHandler */
        $errorHandler = Yii::app()->getErrorHandler();
        if ($errorHandler->shouldRenderErrorAsJson()) {
            // render JSON error representation.
            $errorHandler->renderErrorAsJson();
            
            return;
        }
        
        // Render HTML error representation:
        if ($error = $errorHandler->error) {
            $this->render('error', $error);
        }
    }
}

php composer.phar