Skip to content

Latest commit

 

History

History
89 lines (75 loc) · 2.79 KB

migrate-v2-to-v3.md

File metadata and controls

89 lines (75 loc) · 2.79 KB

Migration V2 to V3

What's changing

The ExceptionFormatService has changed. Now you need to add a new service in your code to extend the default service and override the service.

Code changes

Before

Nothing special in your code

After

  • In your project, create an ExceptionFormatService to handle exceptions you want
  • You have to extend the default toolkit service Openium\SymfonyToolKitBundle\Service\ExceptionFormatService

Example :

<?php
namespace App\Service;

use Openium\SymfonyToolKitBundle\Service\ExceptionFormatService as BaseExceptionFormatService;
use Openium\SymfonyToolKitBundle\Service\ExceptionFormatServiceInterface;

class ExceptionFormatService extends BaseExceptionFormatService implements ExceptionFormatServiceInterface {

    public function genericExceptionResponse(Exception $exception): array
    {
        if (is_a($exception, "Symfony\Component\Security\Core\Exception\AuthenticationException")) {
            $code = Response::HTTP_UNAUTHORIZED;
            $text = Response::$statusTexts[$code];
            $message = $text;
        } elseif (
            is_a($exception, "Firebase\Auth\Token\Exception\ExpiredToken")
            || is_a($exception, "Firebase\Auth\Token\Exception\IssuedInTheFuture")
            || is_a($exception, "Firebase\Auth\Token\Exception\InvalidToken")
        ) {
            // Firebase part
            $code = Response::HTTP_UNAUTHORIZED;
            /* @phpstan-ignore-next-line */
            $text = $exception->getMessage();
            $message = $text;
        } else {
            $code = $this->getStatusCode($exception);
            $text = $this->getStatusText($exception);
            $message = null;
        }
        return [$code, $text, $message];
    }
    /**
     * getGenericArray
     *
     * @param array $error
     * @param Exception $exception
     *
     * @return array
     */
    public function addKeyToErrorArray(array $error, Exception $exception): array
    {
        if (
            $error['status_code'] == Response::HTTP_PAYMENT_REQUIRED
            && $exception->getPrevious() instanceof \Throwable
            && is_a($exception->getPrevious(), "Stripe\Error\Card")
        ) {
            /* @phpstan-ignore-next-line */
            $body = $exception->getPrevious()->getJsonBody();
            $err = $body['error'];
            $error['type'] = $err['type'];
            $error['code'] = $err['code'];
        }
        return $error;
    }
}

Previous example is the old code. You can add or remove cases as you like.

  • To override the service of the bundle, you have to add your own service in config/services.yaml
    For example:
    openium_symfony_toolkit.exception_format:
        class: App\Service\ExceptionFormatService
        arguments:
            - '@kernel'
        public: true