承接 tounaf/exception-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tounaf/exception-bundle

Composer 安装命令:

composer require tounaf/exception-bundle

包简介

Exception handler for symfony project

README 文档

README

If you need to manage complexe exception in your application, this bundle is set for you . This bundle provides interface to facilitate the customization of the exception rendering in Symfony project. Each exception that you create has his own handler classe. Your exception will be scalable, maintenable, independent.

1 - Installation

composer require tounaf/exception-bundle

2 - How to work

By default, this bundle handle exception and render a json response but you can customize this behavior with your needs (ex: html)

3 - Create custom execption handler

Create an class and implement the PulseExceptionInteface interface. It contains two methods:

** handleException: This method have Throwble as argument and returns a Symfony Response

** supportsException: This method have Throwble as argument and return boolean.

Example:

First create Exception to handle through the app.

<?php

namespace App\Handler\Exception;

class MyException extends \Exception 
{
}

Then create the handler that handle this exception

<?php

namespace App\Handler\Exception;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Tounaf\ExceptionBundle\ExceptionHandlerInterface;

class MyExceptionHandler implements ExceptionHandlerInterface 
{
    // return an JsonResponse
    public function handleException(\Throwable $throwable): Response
    {
        // your logic
        return new JsonResponse(['message' => $throwable->getMessage(), 'code' => 12]);
    }

    // 
    public function supportsException(\Throwable $throwable): Response
    {
        return $throwable instanceof MyException;
    }
}

When MyException is thrown, the MyExceptionHandler class is called by the system . For example:

namespace App\Service;

use App\Handler\Exception\MyException;

class MyService
{
    public function someFunction()
    {
        // your logic
        throw new MyException();
    }
}

3 - Format Response

Sometimes, your app provides the service that third party can consume: for example :

/api/users/list returns a json {"data": [{"name":"user1"}, {"name":"user2"}]}

but the uri

/admin/users/list renders an html page

An HandlerException can provide multiple format of response like html, json, etc.
By default, this bundle supports html and json for format of response but it is extensible to create custom format like json-ld, xml etc.
To do that, you need to implement the interface Tounaf\ExceptionBundle\FormatResponse\FormatResponseCheckerInterface.
This interface have a method setFormat(FormatResponseInterface $formatResponse) that accepts FormatResponseInterface as argument.

<?php

namespace App\Handler\Exception;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Tounaf\ExceptionBundle\ExceptionHandlerInterface;
use Tounaf\ExceptionBundle\FormatResponse\FormatResponseCheckerInterface;

class MyExceptionHandler implements ExceptionHandlerInterface, FormatResponseCheckerInterface
{
    private FormatResponseInterface $formatResponse;

    // return an JsonResponse
    public function handleException(\Throwable $throwable): Response
    {
        // your logic
        return $this->formatResponse->render(
            ['message' => $throwable->getMessage()]
        )
    }

    // 
    public function supportsException(\Throwable $throwable): Response
    {
        return $throwable instanceof MyException;
    }

    public function setFormat(FormatResponseInterface $formatResponse): void
    {
        $this->formatResponse = $formatResponse;
    }
}

What happen with this code ? As said at the top, this bundle provides two responses by default : html and json. Which of these formats on this code ? For the URI that match /api/, the response used is json else html is rendered.

tounaf/exception-bundle 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-04