tobento/service-error-handler
Composer 安装命令:
composer require tobento/service-error-handler
包简介
PHP errors and exceptions handling
README 文档
README
The Error Handler Service provides tools to manage errors and exceptions.
Table of Contents
Getting started
Add the latest version of the error handler service project running this command.
composer require tobento/service-error-handler
Requirements
- PHP 8.4 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
Documentation
Error Handling
Check out Throwable Handlers to learn more about the Throwable Handlers.
use Tobento\Service\ErrorHandler\ErrorHandling; use Tobento\Service\ErrorHandler\ThrowableHandlers; use Tobento\Service\ErrorHandler\ThrowableHandlerFactory; use Tobento\Service\ErrorHandler\Handler; $throwableHandlers = new ThrowableHandlers(new ThrowableHandlerFactory()); // adding any handler: // $throwableHandlers->add(ValidationExceptionHandler::class); // $throwableHandlers->add(GeneralExceptionHandler::class); // only on development: $throwableHandlers->add(Handler\Debug::class); // adding last: $throwableHandlers->add(Handler\Errors::class); (new ErrorHandling($throwableHandlers))->register();
Throwable Handlers
The throwable handlers can be used where you need to handle exceptions in general.
Create Throwable Handlers
use Tobento\Service\ErrorHandler\ErrorHandling; use Tobento\Service\ErrorHandler\ThrowableHandlers; use Tobento\Service\ErrorHandler\ThrowableHandlersInterface; use Tobento\Service\ErrorHandler\ThrowableHandlerFactory; $throwableHandlers = new ThrowableHandlers(new ThrowableHandlerFactory()); var_dump($throwableHandlers instanceof ThrowableHandlersInterface); // bool(true)
With autowiring factory
The autowiring factory is needed if you Add Throwable Handlers with dependencies.
use Tobento\Service\ErrorHandler\ErrorHandling; use Tobento\Service\ErrorHandler\ThrowableHandlers; use Tobento\Service\ErrorHandler\ThrowableHandlersInterface; use Tobento\Service\ErrorHandler\AutowiringThrowableHandlerFactory; // Any PSR-11 container $container = new \Tobento\Service\Container\Container(); $throwableHandlers = new ThrowableHandlers( new AutowiringThrowableHandlerFactory($container) ); var_dump($throwableHandlers instanceof ThrowableHandlersInterface); // bool(true)
Add Throwable Handler
By class instance
$throwableHandlers->add(new Handler\Errors());
By class name
$throwableHandlers->add(Handler\Errors::class);
By class name with build-in parameters (not resolvable by autowiring)
$throwableHandlers->add([ThrowableHandler::class, 'name' => 'value']);
By anonymous function
use Throwable; $throwableHandlers->add(function(Throwable $t): mixed { // Return throwable if cannot handle, otherwise anything else. return $t; });
Restrict Throwable Handler
By error level(s)
The throwable handler will only be used on the specified error levels.
$throwableHandlers->add(ThrowableHandler::class) ->level(\E_USER_WARNING, \E_WARNING);
By exception(s)
The throwable handler will only be used on the specified exceptions.
$throwableHandlers->add(ThrowableHandler::class) ->handles(SomeException::class, AnotherException::class);
Prioritize Throwable Handler
You might want to prioritize the excution order of the handlers by the following way (highest first):
$throwableHandlers->add(ThrowableHandler::class) ->priority(1000); // is default
Handle Throwable
use Throwable; try { // do something } catch (Throwable $t) { // do something with the response: $response = $throwableHandlers->handleThrowable($t); }
Handlers
Debug
The debug handler will render a debugging page on any exception not caught.
use Tobento\Service\ErrorHandler\Handler\Debug; use Tobento\Service\View\ViewInterface; use Tobento\Service\ErrorHandler\ThrowableHandlerInterface; $debug = new Debug( view: null, // null|ViewInterface ); var_dump($debug instanceof ThrowableHandlerInterface); // bool(true)
Custom view
Check out View Service to learn more about the View in general.
private/
view/
debug/
error.php
throwable.php
use Tobento\Service\ErrorHandler\Handler\Debug; use Tobento\Service\View\ViewInterface; use Tobento\Service\View\View; use Tobento\Service\View\PhpRenderer; use Tobento\Service\Dir\Dirs; use Tobento\Service\Dir\Dir; $view = new View( new PhpRenderer( new Dirs( new Dir('/private/view'), ) ) ); $debug = new Debug( view: $view, // null|ViewInterface );
Errors
The errors handler will render an error page on shutdown.
use Tobento\Service\ErrorHandler\Handler\Errors; use Tobento\Service\View\ViewInterface; use Tobento\Service\ErrorHandler\ThrowableHandlerInterface; $errors = new Errors( view: null, // null|ViewInterface ); var_dump($errors instanceof ThrowableHandlerInterface); // bool(true)
Custom error view
Check out View Service to learn more about the View in general.
private/
view/
error.php
use Tobento\Service\ErrorHandler\Handler\Errors; use Tobento\Service\View\ViewInterface; use Tobento\Service\View\View; use Tobento\Service\View\PhpRenderer; use Tobento\Service\Dir\Dirs; use Tobento\Service\Dir\Dir; $view = new View( new PhpRenderer( new Dirs( new Dir('/private/view'), ) ) ); $errors = new Errors( view: $view, // null|ViewInterface );
Log
The log handler will log any errors or exceptions.
use Tobento\Service\ErrorHandler\Handler\Log; use Psr\Log\LoggerInterface; use Monolog\Logger; use Monolog\Handler\TestHandler; use Tobento\Service\ErrorHandler\ThrowableHandlerInterface; $logger = new Logger('name'); $logger->pushHandler(new TestHandler()); $log = new Log( logger: $logger, // Closure|LoggerInterface ); var_dump($log instanceof ThrowableHandlerInterface); // bool(true)
Example with Closure and throwable handlers
use Tobento\Service\ErrorHandler\Handler\Log; use Psr\Log\LoggerInterface; use Monolog\Logger; use Monolog\Handler\TestHandler; $throwableHandlers->add(new Log(function(): LoggerInterface { $logger = new Logger('name'); $testHandler = new TestHandler(); $logger->pushHandler($testHandler); return $logger; }))->levels(E_ERROR, E_CORE_ERROR);
Credits
tobento/service-error-handler 适用场景与选型建议
tobento/service-error-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 274 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「exceptions」 「error handler」 「tobento」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tobento/service-error-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tobento/service-error-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tobento/service-error-handler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
php7ify is a project that brings new php7 classes and exceptions to php 5.x.
The CodenamePHP Platform core that contains basic interfaces, exceptions etc..
A simple package to better handle Exceptions.
Monitors errors and exceptions and reports them to Rollbar
Simple ASCII output of array data
统计信息
- 总下载量: 274
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-31