Skip to content

wubinworks/error-handler

Repository files navigation

PHP Error Handler and Exception Handler

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.

Requirements

  • PHP >= 5.3

Installation

composer require wubinworks/error-handler

Usage

ErrorHandler

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

ExceptionHandler

\Wubinworks\ErrorHandler\ExceptionHandler::__construct has 3 parameters and these parameters control the handler's behavior.

  • canOutput Controls whether the handler can output to console/browser.

  • logger Must be type null|\Psr\Log\LoggerInterface. Set to null to disable logging. Note the log level is fixed to ERROR.

  • isTraceIgnoreArgs Controls 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

Unit testing

Install the require-dev dependencies and run the following command.

vendor/bin/phpunit

If you like this package or this package helped you, please share and give a ★☆star☆★, it's NOT hard!

About

Error Handler converts PHP's traditional runtime errors into ErrorException and respects user specified error reporting level. Exception Handler can output and/or log exception message with well-formatted stack trace.

Topics

Resources

License

OSL-3.0, Unknown licenses found

Licenses found

OSL-3.0
LICENSE.txt
Unknown
COPYING.txt

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages