bakame/aide-error
Composer 安装命令:
composer require bakame/aide-error
包简介
A siple class to handle errors in PHP functions
README 文档
README
A Cloak system to help dealing with error reporting in PHP.
Installation
Composer
composer require bakame-php/aide-error
System Requirements
You need:
- PHP >= 8.1 but the latest stable version of PHP is recommended
Usage
Traditionally, to correctly handle errors with PHP's functions, you have two (2) options. Either
you use the @ to suppress the error which is not recommended; or you need to add some
boilerplate code around set_error_handler and restore_error_handler.
The Bakame\Aide\Error\Cloak utility class helps you remove that burden by doing the heavy-lifting for you.
<?php use Bakame\Aide\Error\Cloak; //using the @ suppression operator $res = @touch('/foo'); // bad and not recommended //using error handler set_error_handler(fn (int $errno, string $errstr, string $errfile = null, int $errline = null) { if (!(error_reporting() & $errno)) { // This error code is not included in error_reporting return; } throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); }, E_WARNING); $res = touch('/foo'); restore_error_handler(); // better but having to write this everytime is overkill // and you have little control //using Cloak $touch = Cloak::warning(touch(...)); $res = $touch('/foo'); $touch->errors(); // returns a CloakedErrors instance // the instance is empty on success // otherwise contains all the \ErrorException // generated during the closure execution
You can control its behaviour on your global codebase
<?php use Bakame\Aide\Error\Cloak; Cloak::throwOnError(); try { $touch = Cloak::warning(touch(...)); } catch (ErrorException $exception) }
Or you can decide to specifically change its default behaviour for a specific call.
<?php use Bakame\Aide\Error\Cloak; Cloak::throwOnError(); // by default calls via Cloak should throw if (!$touch = Cloak::warning(touch(...), Cloak::SILENT)) { // errors are still available via the `errors` method // but throwing will not happen $touch->errors(); }
Available properties and methods
Accessing the error
To access the errors store in the instance you need to call the Cloak::errors method
which will return a CloakedErrors instance. This container gives you can access all
the ErrorException generated during the last execution of the callback.
If no error occurred during the last execution of the class, the CloakedErrors instance
will be empty.
$touch = Cloak::all(touch(...)); $touch('/foobar'); $errors = $touch->errors(); // $errors is a CloakedErrors instance $errors->isEmpty(); //true if the execution generated 0 ErrorException; false otherwise foreach ($errors as $error) { $error; //ErrorException instances ordered from the newest to the oldest one. } $errors->first(); // the oldest ErrorException $errors->last(); // the newest ErrorException $errors->get(2); $errors->get(-2); // returns any of the ErrorException and accept negative index. // the three (3) methods will return `null` if no exception // exists for the specified offset
Controlling when to throw or not your errors.
The class general behaviour is controlled by two (2) static methods.
In all cases if an error occurred, it is converted into an ErrorException
and will be made accessible via the Cloak::errors method. The difference
being that with:
Cloak::throwOnError: every instance will throw on the first error;Cloak::silentOnError: no exception will be thrown;
Note
to respect PHP's behaviour, Cloak uses Cloak::silentOnError by default
Named constructors
To ease usage the named constructors are added:
<?php use Bakame\Aide\Error\Cloak; Cloak::env(); // will use the current environment error reporting value // and one for each error reporting level that exists in PHP Cloak::all(); Cloak::error(); Cloak::warning(); Cloak::notice(); Cloak::deprecated(); Cloak::userError(); Cloak::userWarning(); Cloak::userNotice(); Cloak::userDeprecated(); // some Error reporting will never get triggered // they exist for completeness but won't be usable.
They all share the same signature:
static method(Closure $callback, int $onError = Cloak::OBEY);
the $onError argument is used to tweak the instance behaviour on error:
Cloak::THROWwill override the general behaviour and force throwing an exception if availableCloak::SILENTwill override the general behaviour and silence the error if it existsCloak::OBEYwill comply with the curring general behaviour.
If you really need other fined grained error level you can still use the constructor as shown below:
<?php use Bakame\Aide\Error\Cloak; $touch = new Cloak( touch(...), Cloak::THROW, E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED, );
ReportingLevel class
Because dealing with PHP error reporting level can be confusing sometimes, the package ships with an friendlier
approach to deal with them. As an example, the previous code example can be rewritten using the
ReportingLevel class.
<?php use Bakame\Aide\Error\Cloak; use Bakame\Aide\Error\ReportingLevel; $touch = new Cloak( touch(...), Cloak::THROW, ReportingLevel::fromExclusion(E_NOTICE, E_STRICT, E_DEPRECATED), );
The class exposes a friendlier API to ease working with error reporting level:
ReportingLevel::fromValueallow instantiating the class with any value you want.ReportingLevel::fromNameallow instantiating the class with the string corresponding to one of theE_*constants.ReportingLevel::fromEnvinstantiates the class to match your current environment settings.ReportingLevel::fromInclusioninstantiates the error level by adding all the submitted values via a bitwiseORoperation starting at0meaning that no Error reporting level exists if none is added.ReportingLevel::fromExclusiondoes the opposite, each value given will be removed from the maximum value, represented byE_ALL.
on top of that the class expose a construct for each error reporting level using the following syntax:
use Bakame\Aide\Error\ReportingLevel; ReportingLevel::warning()->value(); // returns the same value as E_WARNING. ReportingLevel::userDeprecated()->value(); // returns the same value as E_USER_DEPRECATED. // and so on for each error reporting level
You can tell which error reporting is being configured using the contains method.
The class also provides the excluded and included methods which returns the
error reporting level names.
<?php use Bakame\Aide\Error\ReportingLevel; ReportingLevel::fromEnv()->contains(E_WARNING); // returns true if the current value in error_reporting contains `E_WARNING` // returns false otherwise. $reportingLevel = ReportingLevel::fromInclusion(E_NOTICE, "E_DEPRECATED"); $reportingLevel->value(); // `value` returns the int value corresponding to the calculated error level. // the errorLevel calculated will ignore notice, and deprecated error. $reportingLevel->excluded(); // returns all the error reporting level name no present in the current error Level $reportingLevel->included(); // returns all the error reporting level name present in the current error Level // ["E_NOTICE", "E_DEPRECATED"]
Accessing the Error Reporting Level from a Cloak instance
Once instantiated, you can always access the error reporting level via
the errorLevel method on a Cloak instance. For example, if you need to know if a
specific error is included, you can do the following:
$touch = Cloak::all(touch(...)); $touch->reportingLevel()->contains(E_WARNING); //tells if the E_WARNING is included or not
Credits
bakame/aide-error 适用场景与选型建议
bakame/aide-error 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.73k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「error handler」 「error」 「functions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bakame/aide-error 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bakame/aide-error 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bakame/aide-error 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lazy loading for middleware and request handlers
AMWSCAN (Antimalware Scanner) is a php antimalware/antivirus scanner console script written in php for scan your project. This can work on php projects and a lot of others platform.
Set Links with a specific language parameter
PHP polyfill for the Apache Functions. The aim is to provides functions as the PHP way in order to get them available even if PHP is not running as an Apache module.
Bootstraps errors and handles them via reporters and renderers
PhpStratum SQLite: A stored procedure loader and wrapper generator for SQLite
统计信息
- 总下载量: 1.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-25