Traducciones : Español
Biblioteca PHP para manejar excepciones.
Para manejar excepciones, puede utilizar la biblioteca de manejo de excepciones.
Sistema operativo: Linux | Ventanas.
Versiones de PHP: 8.1.
La forma preferida de instalar esta extensión es a través de Composer.
Para instalar la biblioteca PHP ErrorHandler , simplemente:
composer require josantonius/error-handler
El comando anterior solo instalará los archivos necesarios, si prefieres descargar el código fuente completo puedes usar:
composer require josantonius/error-handler --prefer-source
También puedes clonar el repositorio completo con Git:
git clone https://github.com/josantonius/php-error-handler.git
JosantoniusErrorHandlerErrorException
extiende ErrorException
Obtiene el archivo de error:
public function getFile(): string ;
Obtiene el nivel de error:
public function getLevel(): int ;
Obtiene la línea del archivo de error:
public function getLine(): int ;
Recibe un mensaje de error:
public function getMessage(): string ;
Obtiene el nombre del error:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandled
Obtiene el archivo de error:
public function getFile(): string ;
Obtiene el nivel de error:
public function getLevel(): int ;
Obtiene la línea del archivo de error:
public function getLine(): int ;
Recibe un mensaje de error:
public function getMessage(): string ;
Obtiene el nombre del error:
public function getName(): string ;
JosantoniusErrorHandlerErrorHandler
Convertir errores en excepciones:
/**
* 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 ;
Convierta los errores en excepciones excepto algunos de ellos:
/**
* 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 ;
Registrar la función del controlador de errores:
/**
* 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 ;
Utilice el informe de errores para determinar qué errores se manejan:
/**
* 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 ;
Ejemplos de uso de esta 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 ejecutar pruebas solo necesitas Composer y ejecutar lo siguiente:
git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install
Ejecute pruebas unitarias con PHPUnit:
composer phpunit
Ejecute pruebas estándar de código con PHPCS:
composer phpcs
Ejecute pruebas de PHP Mess Detector para detectar inconsistencias en el estilo del código:
composer phpmd
Ejecute todas las pruebas anteriores:
composer tests
Los cambios detallados para cada versión están documentados en las notas de la versión.
Asegúrese de leer la Guía de contribución antes de realizar una solicitud de extracción, iniciar una discusión o informar un problema.
¡Gracias a todos los contribuyentes! ❤️
Si este proyecto le ayuda a reducir su tiempo de desarrollo, ¿puede patrocinarme para respaldar mi trabajo de código abierto?
Este repositorio tiene la licencia MIT.
Copyright © 2016-presente, Josantonio