app-verk/api-exception-bundle

catch all Symfony 3.3 or newer errors and convert it to problem json RFC7807 response

Installs: 7 163

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 7

Forks: 5

Open Issues: 0

Type:symfony-bundle

1.0.6 2020-12-22 08:07 UTC

This package is auto-updated.

Last update: 2024-11-29 04:30:39 UTC


README

Build Status

Catch all Symfony 3.3 or newer errors and convert it to problem+json RFC7807 response

Installation:

Required the bundle with composer:

$ php composer.phar require app-verk/api-exception-bundle

Add bundle to AppKernel:

<?php
    // app/AppKernel.php

    public function registerBundles()
    {
        $bundles = array(
            // ...
            
            new AppVerk\ApiExceptionBundle\AppVerkApiExceptionBundle(),
        );
    }

Configuration:

AppVerkApiProblemExceptionBundle automatically catch your errors by ApiExceptionSubscriber and return application/problem+json response:

{
  "detail": "Description of problem",
  "status": 404,
  "type": "about:blank",
  "title": "Not Found"
}

changing data structure:

Bundle provide ResponseFactoryInterface for overriding response data, if u want change response data.

Example usage:

to receive response like:

{
  "exception": {
      "detail": "Description of problem",
      "status": 404,
      "type": "about:blank",
      "title": "Not Found"
  }
}

create new CustomResponseFactory:

<?php

...
class FBExceptionResponseFactory implements ResponseFactoryInterface
{
    public function createResponse(ApiProblemInterface $apiProblem)
    {
        $data = $apiProblem->ToArray();

        $response = new JsonResponse(
            $this->prepareData($data)
        );

        $response->headers->set('Content-Type', 'application/problem+json');

        return $response;
    }

    public function prepareData($data)
    {
        return [
            'exception' => [
                $data
            ],
        ];
    }
}

Config reference:

app_verk_api_exception:
    response_factory: AppVerk\ApiExceptionBundle\Factory\ApiProblemResponseFactory
    enabled: true
    paths_excluded: ['/admin/']