wikimedia/normalized-exception
Composer 安装命令:
composer require wikimedia/normalized-exception
包简介
A helper for making exceptions play nice with PSR-3 logging
README 文档
README
NormalizedException
A minimal library to facilitate PSR-3-friendly exception handling.
Additional documentation about the library can be found on MediaWiki.org.
Usage
The NormalizedException library provides an INormalizedException interface that exception classes can use to support a standardized way of logging their error messages in a PSR-3 compatible manner.
If you don't care about the exception class, you can use the standard implementation:
use Wikimedia\NormalizedException\NormalizedException; throw new NormalizedException( 'Invalid value: {value}', [ 'value' => $value ] );
use Wikimedia\NormalizedException\INormalizedException; try { mightThrow(); } catch ( INormalizedException $e ) { $psr3Logger->error( $e->getNormalizedMessage(), $e->getMessageContext() ); echo 'Error: ' . $e->getMessage(); }
To make a specific exception class normalized, if it's constructed from a message and optionally an error code and a previous exception (like most PHP core exceptions), you can use a trait and a default constructor provided by that trait:
use Exception use Wikimedia\NormalizedException\INormalizedException; use Wikimedia\NormalizedException\NormalizedExceptionTrait; class MyException extends Exception implements INormalizedException { use NormalizedExceptionTrait { NormalizedExceptionTrait::normalizedConstructor as __construct; } }
throw new MyException( 'Invalid value!' ); throw new MyException( 'Invalid value: {value}', [ 'value' => $value ] ); throw new MyException( 'Invalid value: {value}', [ 'value' => $value ], /* code */ -1, $previous );
Exceptions with different parameters need their own constructor:
use Wikimedia\NormalizedException\INormalizedException; use Wikimedia\NormalizedException\NormalizedExceptionTrait; class MyException extends SomeException implements INormalizedException { use NormalizedExceptionTrait; public function __construct( string $normalizedMessage, array $messageContext = [], $otherParam1, $otherParam2 ) { // these properties are defined by the trait and must be set $this->normalizedMessage = $normalizedMessage; $this->messageContext = $messageContext; // replaces the PSR-3 tokens $message = self::getMessageFromNormalizedMessage( $normalizedMessage, $messageContext ); parent::__construct( $message, $otherParam1, $otherParam2 ); } }
Running tests
composer install --dev
composer test
History
This library was split out of MediaWiki changeset 670465 during the MediaWiki 1.37 development cycle.
wikimedia/normalized-exception 适用场景与选型建议
wikimedia/normalized-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 419.1k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wikimedia/normalized-exception 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wikimedia/normalized-exception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 419.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-01