定制 phphd/exception-handler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phphd/exception-handler

Composer 安装命令:

composer require phphd/exception-handler

包简介

Message Bus Exception Handler

README 文档

README

🧰 PHP Exception Handler built on top of Symfony Messenger component. Provides middlewares that allow you to easily re-raise exceptions, chain them, or handle with a dedicated handler.

Build Status Codecov Psalm coverage Psalm level Packagist Downloads Licence

Installation 📥

  1. Install via composer

    composer require phphd/exception-handler
  2. Enable the bundle in the bundles.php

    PhPhD\ExceptionHandler\Bundle\PhdExceptionHandlerBundle::class => ['all' => true],

Configuration ⚒️

To leverage features of this bundle, you should add phd_exception_handler middleware to the list:

framework:
    messenger:
        buses:
            command.bus:
                default_middleware: false
                middleware:
+                   - phd_exception_handler
                    - validation
                    - doctrine_transaction

The core principle of exception handling revolves around the idea that exceptions are dispatched to the corresponding bus to be handled. There must be one exception bus per one origin bus.

The exception bus name convention is straightforward: command.bus exceptions are forwarded into command.exception.bus.

framework:
    messenger:
        buses:
            command.exception.bus:
                default_middleware: false
                middleware:
                    - phd_exception_rethrow_unhandled
                    - phd_exception_chaining
                    - phd_exception_result_filter
                    -   handle_message:
                            - true

Currently, there are few exception handling middlewares provided.

Rethrowing unhandled

Middleware: phd_exception_rethrow_unhandled

In case if dispatched exception had not been handled it is rethrown back. The exception is considered as handled if handler returns a response, or throws another exception.

Exception chaining

Middleware: phd_exception_chaining

Implements automatic exceptions escalation logic with #[RaiseAs] attribute.

Result filter

Middleware: phd_exception_result_filter

Filters out all null results of exception handlers.

Usage 🚀

Re-Raising Exceptions

The simplest use-case is defining #[RaiseAs] attribute on your exception class:

use PhPhD\ExceptionHandler\Middleware\Chain\Escalator\RaiseAs;

#[RaiseAs(AccessDeniedHttpException::class, bus: 'api.exception.bus')]
final class NonWhiteListedUserException extends DomainException
{
}

In this example, any time NonWhiteListedUserException is thrown from an underlying handler, it will be raised as AccessDeniedHttpException.

As you can see, there's required attribute bus option. Since some exceptions could be thrown from multiple different contexts (hence, different buses), it is required to explicitly specify the bus from which the particular exception must be raised, so that in other scenarios another exceptions could be escalated:

use PhPhD\ExceptionHandler\Middleware\Chain\Escalator\RaiseAs;

#[RaiseAs(ImportLockedHttpException::class, bus: 'api.exception.bus')]
#[RaiseAs(RecoverableMessageHandlingException::class, bus: 'consumer.exception.bus')]
final class ImportLockedException extends RuntimeException
{
}

In this example, ImportLockedException could be thrown either in http context (api.bus), or in the mq consumer context (consumer.bus). Therefore, raised exceptions are different.

Manual Handling

The exception is dispatched down to your custom handlers, where you could either return a Response, throw a new exception, or just log it and return null so that exception will be re-thrown again.

#[AsMessageHandler('api.exception.bus')]
final readonly class InventoryExceptionHandler
{
    /** @throws Throwable */
    public function __invoke(InventoryDomainException $exception, InventoryCommand $command): ?Response
    {
        if ($exception instanceof JournalHasUnInventoriedItemException) {
            $data = $this->formatJournalException($exception);

            return new JsonResponse($data, Response::HTTP_BAD_REQUEST);
        }

        if ($exception instanceof StockItemNotVaildatedException) {
            $data = $this->formatItemException($exception, $command->getJournal());

            throw new StockItemNotValidatedHttpException($data, $exception);
        }

        return null;
    }
}

If you would like to use the same exception handler for multiple exception buses, you can do so by adding multiple #[AsMessageHandler] attributes:

#[AsMessageHandler(bus: 'command.exception.bus')]
#[AsMessageHandler(bus: 'query.exception.bus')]
final readonly class InventoryExceptionHandler
{
    // ...
}

phphd/exception-handler 适用场景与选型建议

phphd/exception-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.15k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 phphd/exception-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phphd/exception-handler 我们能提供哪些服务?
定制开发 / 二次开发

基于 phphd/exception-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-14