tounaf / exception-bundle
Exception handler for symfony project
Installs: 108
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 2
Open Issues: 2
Type:symfony-bundle
Requires
- symfony/config: 6.4.* || 6.3.* || 6.2.* || 6.1.* || 5.4.* || 4.4.*
- symfony/dependency-injection: 6.4.* || 6.3.* || 6.2.* || 6.1.* || 5.4.* || 4.4.*
- symfony/framework-bundle: 6.4.* || 6.3.* || 6.2.* || 6.1.* || 5.4.* || 4.4.*
- symfony/http-kernel: 6.4.* || 6.3.* || 6.2.* || 6.1.* || 5.4.* || 4.4.*
- tounaf/exception-handler: ^1.4
Requires (Dev)
- enlightn/security-checker: ^1.11
- friendsofphp/php-cs-fixer: ^3.39
- rector/rector: ^1.0
- squizlabs/php_codesniffer: *
- symfony/maker-bundle: ^1.48
- symfony/test-pack: ^1.1
- symfony/var-dumper: ^6.4 || ^6.3 || ^6.2
This package is auto-updated.
Last update: 2024-11-19 15:01:50 UTC
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.