This bundle can listen to exception that reach the kernel of Symfony to transform them before the kernel render them.
This is useful when you want to transform domain/business exception from your code to presentation exception such as http exception that contain http status code., (*1)
, (*2)
Usage
Require the dependency in your composer.json
For symfony >=4 and php >=7.4, (*3)
"riper/exception-transformer" : "2.*"
For symfony <4 and php <7.4, (*4)
"riper/exception-transformer" : "1.*"
Register the bundle in app-kernel
new Riper\Bundle\ExceptionTransformerBundle\RiperCommonExceptionTransformerBundle(),
Implement the interface \Riper\Bundle\ExceptionTransformerBundle\ExceptionTransformer\ExceptionTransformerInterface, (*5)
Implement the transform method. It can (if required) throw a new exception according to the exception given., (*6)
Example :, (*7)
<?php
namespace Riper\Bundle\Moderation\AmoBundle\Exceptions;
use Riper\Bundle\Accounts\AuthenticationBundle\Exceptions\NotFoundException;
use Riper\Bundle\ExceptionTransformerBundle\ExceptionTransformer\ExceptionTransformerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExceptionTransformer implements ExceptionTransformerInterface
{
/**
* {@inheritdoc}
*/
public function transform(\Exception $exception)
{
switch (1){
case $exception instanceof NotFoundException :
throw new NotFoundHttpException($exception->getMessage(),$exception);
case $exception instanceof InvalidParametersException :
throw new BadRequestHttpException($exception->getMessage(),$exception);
}
}
}
````
Create a service description tagged with the following information
* name : riper.exception.transformer
* scope: [The namespace the transformer will be restricted to]
example :
```yaml
riper.moderation.exception.transformer:
class: Riper\Bundle\biduleBundle\Exceptions\ExceptionTransformer
tags:
- { name: riper.exception.transformer , namespace_scope: "Riper\\Bundle\\Bidule\\"}
You can transform your exceptions in httpException from symfony/http-kernel just by adding some configuration. No code needed., (*8)
- Create a configuration key in parameters with a unique name ending by "riper_exception_map".
- The parameter contain a key=>value with
- key = THe full namespace of the exception to transform (without a slash at the beginning)
- value = The http status to generate (an exception will be thrown with the proper http status code)
The list of available status is located in riper_exception_mapping.shortcuts parameter in ExceptionTransformerBundle/Resources/config/exceptions_mapping.yml.
The following is a non-exhaustive list, (*9)
- BadRequestHttpException
- NotFoundHttpException
- ConflictHttpException
- AccessDeniedHttpException
- GoneHttpException
- LengthRequiredHttpException
- MethodNotAllowedHttpException
- NotAcceptableHttpException
- PreconditionFailedHttpException
- PreconditionRequiredHttpException
- UnprocessableEntityHttpException
- UnsupportedMediaTypeHttpException
Example :, (*10)
parameters:
customercare.contact.riper_exception_map:
Riper\Bundle\CustomerCare\ContactBundle\Exceptions\InvalidParameterException: BadRequestHttpException
Riper\Bundle\CustomerCare\ContactBundle\Exceptions\ContactException: BadRequestHttpException
The status code/exception is not in the shortcut list ? use the first method with the transformer tagged, (*11)