tounaf/exception-handler
最新稳定版本:v1.4.0
Composer 安装命令:
composer require tounaf/exception-handler
包简介
Exception handler for symfony project
README 文档
README
Create handler class for each exception that you throw in your application.
1 - Installation
composer require tounaf/exception-handler
2 - Features
this package allows to separate the handler class by exception to avoid code coupling : - One classe handler is responsible for exception - Render response throught different output (html, json, xml) - Decorate classe handler for other needs (Write to log file, send exception to mail, to sentry)
3 - Create Exception Handler class
Create an class and implement the ExceptionHandlerInteface 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\Exception\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(); } }
统计信息
- 总下载量: 54
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-18