PHP code example of programulin / errors-handler

1. Go to this page and download the library: Download programulin/errors-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/ */

    

programulin / errors-handler example snippets


use Programulin\ErrorHandler\ErrorHandler;

$handler = new ErrorHandler();

$handler->register(function($message, $exception) {
	var_dump($message);
	var_dump($exception);
});

// Аналог ini_set('display_errors', 'off');
$handler->setDisplayErrors('off');

// Аналог error_reporting(E_ALL);
$handler->setErrorReporting(E_ALL);

// Перечисляем уровни ошибок, которые наш обработчик должен игнорировать
$handler->disallow([E_NOTICE, E_STRICT]);

// Превращает не фатальные ошибки в исключения
$handler->throwErrors(true);

(new ErrorHandler())->register(function($message, $exception) {
	var_dump($message);
	var_dump($exception);
})
	->setDisplayErrors('off')
	->setErrorReporting(E_ALL)
	->disallow([E_STRICT])
	->throwErrors(true);


use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Programulin\ErrorHandler\ErrorHandler;

$logger = new Logger('errors');
$logger->pushHandler(new StreamHandler('errors.log'));

(new ErrorHandler())->register(function($message, $exception) use ($logger) {
	$logger->error($message, [
		'exception' => $exception,
		'uri' => $_SERVER['REQUEST_URI']
	]);
})
	->setDisplayErrors('off')
	->setErrorReporting(E_ALL)
	->disallow([E_STRICT])
	->throwErrors(true);

(new ErrorHandler())->register(function($message){
	var_dump($message);
})->throwErrors(true);

// Поскольку $title не определена, эта строка вызовет ошибку E_NOTICE, которая превратится в исключение
echo $title;

// Эта строка кода уже не будет выполнена
echo 'test';

(new ErrorHandler())->register(function($message){
	var_dump($message);
})->throwErrors(true);

try {
	echo $title;
} catch(\Exception $e) {
	echo 'Ошибка отловлена';
}

// Теперь эта строка будет выполнена
echo 'test';