2017 © Pedro Peláez
 

symfony-bundle exception-transformer

Bundle to transform exception before the kernel catch them

image

riper/exception-transformer

Bundle to transform exception before the kernel catch them

  • Wednesday, June 22, 2016
  • by ztec
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,464 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Exception transformer

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)

License CC-BY-4 php version symfony version, (*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(),

Transform exception with a service (most portable way)

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\\"}

Use the built-in transformer for http error exception

You can transform your exceptions in httpException from symfony/http-kernel just by adding some configuration. No code needed., (*8)

  1. Create a configuration key in parameters with a unique name ending by "riper_exception_map".
  2. 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)

The Versions

22/06 2016

dev-master

9999999-dev https://github.com/RiperFr/ExceptionTransformerBundle

Bundle to transform exception before the kernel catch them

  Sources   Download

CC-BY-4.0

The Requires

 

symfony exception kernel httpexception errorcode

22/06 2016

v1.0.1

1.0.1.0 https://github.com/RiperFr/ExceptionTransformerBundle

Bundle to transform exception before the kernel catch them

  Sources   Download

CC-BY-4.0

The Requires

 

symfony exception kernel httpexception errorcode

22/06 2016

v1.0.0

1.0.0.0 https://github.com/RiperFr/ExceptionTransformerBundle

Bundle to transform exception before the kernel catch them

  Sources   Download

CC-BY-4.0

The Requires

 

symfony exception kernel httpexception errorcode