Traduções : espanhol
Biblioteca PHP para tratamento de exceções.
Para lidar com exceções, você pode usar a biblioteca manipuladora de exceções.
Sistema operacional: Linux | Windows.
Versões do PHP: 8.1.
A forma preferida de instalar esta extensão é através do Composer.
Para instalar a biblioteca PHP ErrorHandler , simplesmente:
composer require josantonius/error-handler
O comando anterior irá instalar apenas os arquivos necessários, caso prefira baixar todo o código fonte você pode usar:
composer require josantonius/error-handler --prefer-source
Você também pode clonar o repositório completo com Git:
git clone https://github.com/josantonius/php-error-handler.git
JosantoniusErrorHandlerErrorException
Estende ErrorException
Obtém arquivo de erro:
public function getFile(): string ;
Obtém o nível de erro:
public function getLevel(): int ;
Obtém a linha do arquivo de erro:
public function getLine(): int ;
Recebe mensagem de erro:
public function getMessage(): string ;
Obtém o nome do erro:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandled
Obtém arquivo de erro:
public function getFile(): string ;
Obtém o nível de erro:
public function getLevel(): int ;
Obtém a linha do arquivo de erro:
public function getLine(): int ;
Recebe mensagem de erro:
public function getMessage(): string ;
Obtém o nome do erro:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandler
Converta erros em exceções:
/**
* 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.
*/
public function convertToExceptions( int ... $ errorLevel ): ErrorHandler ;
Converta erros em exceções, exceto alguns deles:
/**
* 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.
*/
public function convertToExceptionsExcept( int ... $ errorLevel ): ErrorHandler ;
Função de tratamento de erros de registro:
/**
* The error handler will receive the ErrorHandled object.
*
* @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
*/
public function register( callable $ callback ): ErrorHandler ;
Use o relatório de erros para determinar quais erros serão tratados:
/**
* 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
*/
public function useErrorReportingLevel(): ErrorHandler ;
use Josantonius ErrorHandler Exceptions WrongErrorLevelException ;
Exemplos de uso desta biblioteca:
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.
Para rodar testes você só precisa do compositor e executar o seguinte:
git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install
Execute testes unitários com PHPUnit:
composer phpunit
Execute testes padrão de código com PHPCS:
composer phpcs
Execute testes do PHP Mess Detector para detectar inconsistências no estilo do código:
composer phpmd
Execute todos os testes anteriores:
composer tests
As alterações detalhadas de cada versão estão documentadas nas notas de versão.
Certifique-se de ler o Guia de Contribuição antes de fazer uma solicitação pull, iniciar uma discussão ou relatar um problema.
Obrigado a todos os contribuidores! ❤️
Se este projeto ajudar você a reduzir seu tempo de desenvolvimento, você pode me patrocinar para apoiar meu trabalho de código aberto?
Este repositório está licenciado sob a licença MIT.
Copyright © 2016-presente, Josantonius