php error handler
v2.0.2

翻譯:西班牙語
用於處理異常的 PHP 函式庫。
若要處理異常,您可以使用異常處理程序庫。
作業系統:Linux |視窗。
PHP 版本:8.1。
安裝此擴充功能的首選方法是透過 Composer。
要安裝PHP ErrorHandler 庫,只需:
composer require josantonius/error-handler前面的命令只會安裝必要的文件,如果您想下載完整的源代碼,您可以使用:
composer require josantonius/error-handler --prefer-source您也可以使用 Git克隆完整的儲存庫:
git clone https://github.com/josantonius/php-error-handler.git JosantoniusErrorHandlerErrorException擴充了 ErrorException
取得錯誤檔案:
public function getFile(): string ;取得錯誤等級:
public function getLevel(): int ;取得錯誤文件行:
public function getLine(): int ;取得錯誤訊息:
public function getMessage(): string ;取得錯誤名稱:
public function getName(): string ;JosantoniusErrorHandlerErrorHandled
取得錯誤檔案:
public function getFile(): string ;取得錯誤等級:
public function getLevel(): int ;取得錯誤文件行:
public function getLine(): int ;取得錯誤訊息:
public function getMessage(): string ;取得錯誤名稱:
public function getName(): string ;JosantoniusErrorHandlerErrorHandler
將錯誤轉換為異常:
/**
* 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 ;將錯誤轉換為異常,但其中一些錯誤除外:
/**
* 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 ;註冊錯誤處理函數:
/**
* 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 ;使用錯誤報告來確定處理哪些錯誤:
/**
* 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 ;該庫的使用範例:
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. 要執行測試,您只需要 Composer 並執行以下命令:
git clone https://github.com/josantonius/php-error-handler.git cd php-error-handler composer install使用 PHPUnit 執行單元測試:
composer phpunit使用 PHPCS 執行程式碼標準測試:
composer phpcs執行 PHP Mess Detector 測試來偵測程式碼樣式中的不一致:
composer phpmd運行之前的所有測試:
composer tests 每個版本的詳細變更都會記錄在發行說明中。
在發出拉取請求、開始討論或回報問題之前,請務必閱讀貢獻指南。
感謝所有貢獻者! ❤️
如果這個專案可以幫助您減少開發時間,您可以資助我支持我的開源工作嗎?
該儲存庫已根據 MIT 許可證獲得許可。
版權所有 © 2016 年至今,Josantonius