定制 tounaf/exception-bundle 二次开发

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

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

tounaf/exception-bundle

最新稳定版本:v2.2.1

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固