Translations: Español
PHP library for handling exceptions.
To handle exceptions you can use the exception-handler library.
- Requirements
- Installation
- Available Classes
- Exceptions Used
- Usage
- Tests
- TODO
- Changelog
- Contribution
- Sponsor
- License
This library is compatible with the PHP versions: 8.1.
The preferred way to install this extension is through Composer.
To install PHP ErrorHandler library, simply:
composer require josantonius/error-handler
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require josantonius/error-handler --prefer-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-error-handler.git
/**
* This class extends from ErrorException.
*
* @see https://www.php.net/manual/en/class.errorexception.php
*/
use Josantonius\ErrorHandler\ErrorException;
Gets error file:
$errorHandler->getFile(): string
Gets error level:
$errorHandler->getLevel(): int
Gets error file line:
$errorHandler->getLine(): int
Gets error message:
$errorHandler->getMessage(): string
Gets error name:
$errorHandler->getName(): string
use Josantonius\ErrorHandler\ErrorHandled;
Gets error file:
$errorHandled->getFile(): string
Gets error level:
$errorHandled->getLevel(): int
Gets error file line:
$errorHandled->getLine(): int
Gets error message:
$errorHandled->getMessage(): string
Gets error name:
$errorHandled->getName(): string
use Josantonius\ErrorHandler\ErrorHandler;
Create object:
$errorHandler = new ErrorHandler();
Convert errors to exceptions:
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
$errorHandler->convertToExceptions(int ...$errorLevel): ErrorHandler
Convert errors to exceptions except for some of them:
/**
* The errors will be thrown from the ErrorException instance.
*
* @param int[] $errorLevel Define the specific error levels that will become exceptions.
*
* @throws WrongErrorLevelException if error level is not valid.
*
* @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
*/
$errorHandler->convertToExceptionsExcept(int ...$errorLevel): ErrorHandler
Register error handler function:
/**
* The error handler will receive the ErrorHandled object.
*
* @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
*/
$errorHandler->register(callable $callback): ErrorHandler
Use error reporting to determine which errors are handled:
/**
* If the setting value in error_reporting() is used to determine which errors are handled.
*
* If this method is not used, all errors will be sent to the handler.
*
* @see https://www.php.net/manual/en/function.error-reporting.php
*/
$errorHandler->useErrorReportingLevel(): ErrorHandler
use Josantonius\ErrorHandler\Exceptions\WrongErrorLevelException;
Examples of use for this library:
use Josantonius\ErrorHandler\ErrorHandler;
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions();
// All errors will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandler;
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions(E_USER_ERROR, E_USER_WARNING);
// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandler;
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptionsExcept(E_USER_DEPRECATED, E_USER_NOTICE);
// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandler;
error_reporting(E_USER_ERROR);
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions()->useErrorReportingLevel();
// Only E_USER_ERROR will be converted to exception.
use ErrorException;
use Josantonius\ErrorHandler\ErrorHandler;
set_exception_handler(function (ErrorException $exception) {
var_dump([
'level' => $exception->getLevel(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'name' => $exception->getName(),
]);
});
$errorHandler = new ErrorHandler();
$errorHandler->convertToExceptions();
// All errors will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;
function handler(Errorhandled $errorHandled): void {
var_dump([
'level' => $errorHandled->getLevel(),
'message' => $errorHandled->getMessage(),
'file' => $errorHandled->getFile(),
'line' => $errorHandled->getLine(),
'name' => $errorHandled->getName(),
]);
}
$errorHandler = new ErrorHandler();
$errorHandler->register(
callback: handler(...)
);
// All errors will be converted to exceptions.
use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;
class Handler {
public static function errors(Errorhandled $exception): void { /* do something */ }
}
$errorHandler = new ErrorHandler();
$errorHandler->register(
callback: Handler::errors(...)
)->convertToExceptions();
// The error will be sent to the error handler and then throw the exception.
error_reporting(E_USER_ERROR);
class Handler {
public function errors(Errorhandled $exception): void { /* do something */ }
}
$handler = new Handler();
$errorHandled->register(
callback: $handler->errors(...),
)->convertToExceptions()->useErrorReportingLevel();
// Only E_USER_ERROR will be passed to the handler and converted to exception.
To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install
Run unit tests with PHPUnit:
composer phpunit
Run code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
Detailed changes for each release are documented in the release notes.
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
This repository is licensed under the MIT License.
Copyright © 2016-present, Josantonius