krmgns/errorise
Composer 安装命令:
composer require krmgns/errorise
包简介
An elegant way to handle errors in PHP.
关键字:
README 文档
README
If you are tired of using the @ error suppression operator and then digging for errors with the error_get_last() function, you can try using Errorise. Errorise offers well-caught PHP errors to handle them on your side, freeing you from these stuff for each error-prone call.
Installing
composer require krmgns/errorise
Using ErrorHandler
use Errorise; $eh = new Errorise\ErrorHandler(); try { fopen('/path/to/file.txt', 'r'); // Throws if any error occured. $eh->throw(); } catch (Errorise\ErrorException $e) { // Message: fopen(/path/to/file.txt): Failed to open ... throw new YourCustomException_After_Some_Business( $e->getMessage() ); } finally { // Trigger handler __destruct() to call unregister(). unset($eh); }
Using ErrorHandler for Specific Functions / Patterns
You can controll that when to throw or for which function or message pattern to throw.
try { fopen('/path/to/file.txt', 'r'); // Throws if any error occured with fopen(). $eh->throwFor('fopen'); // Throws if any error occured with message pattern. $eh->throwForMatch('/fopen/'); } catch (Errorise\ErrorException $e) { // ... } finally { // ... }
Using ErrorHandler for Undefined Variables
Like for function errors, ErrorHandler is available for undefined variable errors as well:
try { $bar = $foo; // Throws since $foo is undefined. $eh->throw(); } catch (Errorise\ErrorException $e) { // ... } finally { // ... }
Using ErrorHandler with Non-Auto Mode
If you want full controll on register / unregister routine, pass $auto argument as false, just like:
$eh = new Errorise\ErrorHandler(false); try { // Register Errorise error handler. $eh->register(); // Some risky or error-prone works. // Throws if any error occured. $eh->throw(); } catch (Errorise\ErrorException $e) { // ... } finally { // Un-Register Errorise error handler. // So, back to the previous or internal error handler. $eh->unregister(); }
Getting Error Messages in Catch
You can get error messages by using two methods of caught ErrorException.
try { // ... } catch (Errorise\ErrorException $e) { // Message: mkdir(): No such file or directory $e->getMessage(); // Message: No such file or directory $e->getPureMessage(); } finally { // ... }
Utilising Error Object
To get more details, you can utilise the $error property of the ErrorHandler which is passed to the caught ErrorException.
try { // ... } catch (Errorise\ErrorException $e) { // @var Errorise\Error $error = $e->error(); // Data: [severity, message, file, line] $data = $error->data(); // Severity: 2 $error->getSeverity(); // Message: mkdir(): No such file or directory $error->getMessage(); // File: /tmp/php/errorise/test.php $error->getFile(); // Line: 3, where mkdir() was called. $error->getLine(); // Function / Variable Name. $error->getFunction(); $error->getVariable(); } finally { // ... }
Using ErrorWrapper
You can use ErrorWrapper to wrap your calls instead of using try/catch blocks.
$ret = Errorise\ErrorWrapper::wrap(function () { $fp = fopen('/path/to/file.txt', 'r'); return $fp; }, $e /* byref */); assert($ret == false); assert($e instanceof Errorise\ErrorException);
Manually Handling the Last Errors
You can use LastErrorException to throw errors after checking your call results.
use Errorise; // Your filesystem module. class FileSystem { public static function createDirectory( string $dir, int $mode = 0777, bool $recursive = false ): void { $ok = @mkdir($dir, $mode, $recursive); if (!$ok) { throw new Errorise\LastErrorException(); } } } // Your client layer. try { FileSystem::createDirectory('/tmp'); } catch (Errorise\LastErrorException $e) { // Message: mkdir(): File exists throw new YourCustomException_After_Some_Business( $e->getMessage() ); }
krmgns/errorise 适用场景与选型建议
krmgns/errorise 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「error handling」 「error」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krmgns/errorise 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krmgns/errorise 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krmgns/errorise 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides form handler for Symfony form types
PHP Error Handler module that captures and displays all throwable errors in a given format, making debugging easier and more efficient
Common classes for handling files
Error and Exception override and observers.
Error handler based on Booboo with HTML and JSON support
Error Share App, for handle error page
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-01-26