This package contains \Wubinworks\ErrorHandler\ErrorHandler and \Wubinworks\ErrorHandler\ExceptionHandler.
The \Wubinworks\ErrorHandler\ErrorHandler::handle converts PHP's traditional runtime errors into \ErrorException and respects user specified error reporting level.
\Wubinworks\ErrorHandler\ErrorHandler::handle can be used as a callback for set_error_handler directly.
The \Wubinworks\ErrorHandler\ExceptionHandler::handle handles any \Throwable and controls whether to output exception message and logging. You can also get the exception message with well-formatted stack trace information as string.
\Wubinworks\ErrorHandler\ExceptionHandler::handle can be used as a callback for set_exception_handler directly.
- PHP >= 5.3
composer require wubinworks/error-handlerJust set_error_handler([new \Wubinworks\ErrorHandler\ErrorHandler(), 'handle']).
This handler respects the error_reporting level set by user and if the traditional runtime error needs to be reported, an \ErrorException will be thrown. The \ErrorException's severity level is set to the handled error level.
Example:
// Set the error reporting level you prefer
$previousErrorReportingLevel = error_reporting(/** This is just an example level */ E_ALL & ~E_WARNING);
set_error_handler([new \Wubinworks\ErrorHandler\ErrorHandler(), 'handle']);
try {
// Code like `fopen` that can trigger a traditional runtime error
// Here we use `trigger_error` for convenience
trigger_error('The error message.', E_USER_NOTICE);
} catch (\ErrorException $e) {
var_dump($e->getMessage());
// The error message.
var_dump($e->getSeverity());
// Value of predefined constant E_USER_NOTICE(1024)
}
// Error control operator
@trigger_error('You will not see this message. No error, no exception.', E_USER_NOTICE);
restore_error_handler();
error_reporting($previousErrorReportingLevel);\Wubinworks\ErrorHandler\ExceptionHandler::__construct has 3 parameters and these parameters control the handler's behavior.
-
canOutputControls whether the handler can output to console/browser. -
loggerMust be typenull|\Psr\Log\LoggerInterface. Set tonullto disable logging. Note the log level is fixed toERROR. -
isTraceIgnoreArgsControls whether to include function/method parameter information in the output/log.
Example:
try {
...
if (...) {
throw new \Exception('The exception message.');
}
...
} catch (\Throwable $e) {
$logger = new \Monolog\Logger('example-logger');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('<path/to/your.log>'));
$exceptionHandler = new \Wubinworks\ErrorHandler\ExceptionHandler(
false, // No output
$logger, // PSR-3 Logger Interface
false // Stack trace does not include function/method parameters
);
var_dump($exceptionHandler->getMessage($e));
// Exception message + exception class + stack trace
$exceptionHandler->handle($e);
// No console/browser output but the exception message and stack trace information should have been written to <path/to/your.log>.
}You can also do like this:
// Replace the PHP's built-in exception handler
set_exception_handler([new \Wubinworks\ErrorHandler\ExceptionHandler(true, null, false), 'handle']);The output/log include exception message, exception class and well-formatted stack trace information.
Example output/log:
Exception: Test exception message in file /project/ErrorHandler/tests/Test/Unit/ExceptionHandlerTest.php on line 75
Stack trace:
1. Exception->() /project/ErrorHandler/tests/Test/Unit/ExceptionHandlerTest.php:75
2. Wubinworks\ErrorHandler\Test\Unit\ExceptionHandlerTest->testHandleAndGetMessage() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:1311
3. PHPUnit\Framework\TestCase->invokeTestMethod() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:1327
4. PHPUnit\Framework\TestCase->runTest() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:452
5. PHPUnit\Framework\TestCase->runBare() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php:110
6. PHPUnit\Framework\TestRunner\TestRunner->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:301
7. PHPUnit\Framework\TestCase->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
8. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
9. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
10. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
11. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:64
12. PHPUnit\TextUI\TestRunner->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/TextUI/Application.php:254
13. PHPUnit\TextUI\Application->run() /project/ErrorHandler/vendor/phpunit/phpunit/phpunit:104
14. include() /project/ErrorHandler/vendor/bin/phpunit:122
Install the require-dev dependencies and run the following command.
vendor/bin/phpunitIf you like this package or this package helped you, please share and give a ★☆star☆★, it's NOT hard!