FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 703 forks source link

How to handle exception in 2.8 ? #2224

Open vincentjdc opened 4 years ago

vincentjdc commented 4 years ago

I'm working on a project with symfony 4.4 and FosRestBundle 2.7 and JMSSerializerBundle.

I just upgrade FOSRestBundle from 2.7 to 2.8.0-beta3.

I run some functional tests, all tests pass but got lot of deprecation warnings :

4x: The "Doctrine\Common\Inflector\Inflector::classify" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListForCountry from App\Tests\Controller 1x in PublicHolidayControllerTest::testListForCountryAndYear from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: Enabling the route generation feature is deprecated since FOSRestBundle 2.8. 2x in PublicHolidayControllerTest::setUp from App\Tests\Controller

2x: Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8. 2x in PublicHolidayControllerTest::setUp from App\Tests\Controller

2x: Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8. 2x in PublicHolidayControllerTest::setUp from App\Tests\Controller

2x: The body_listener config has been enabled by default and will be disabled by default in FOSRestBundle 3.0. Please enable or disable it explicitly. 2x in PublicHolidayControllerTest::setUp from App\Tests\Controller

1x: The "controller_name_converter" service is deprecated since Symfony 4.3. It is being referenced by the "fos_rest.routing.loader.controller" service. 1x in PublicHolidayControllerTest::setUp from App\Tests\Controller

1x: The "fos_rest.inflector" service alias is deprecated since FOSRestBundle 2.8. It is being referenced by the "fos_rest.routing.loader.reader.action" service. 1x in PublicHolidayControllerTest::setUp from App\Tests\Controller

1x: The FOS\RestBundle\EventListener\ExceptionListener class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::setUp from App\Tests\Controller

1x: The FOS\RestBundle\Serializer\Normalizer\ExceptionHandler class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::setUp from App\Tests\Controller

Other deprecation notices (39)

8x: The "fos_rest.exception_listener" service is deprecated since FOSRestBundle 2.8. 2x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 2x in PublicHolidayControllerTest::testListForCountry from App\Tests\Controller 2x in PublicHolidayControllerTest::testListForCountryAndYear from App\Tests\Controller 2x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.directory service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.xml_collection service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.processor service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.controller service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.reader.controller service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.reader.action service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.inflector.doctrine service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: Not passing an instance of FOS\RestBundle\Routing\Loader\Reader\RestControllerReader as the 3rd argument of FOS\RestBundle\Routing\Loader\RestRouteLoader::__construct() is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

2x: The fos_rest.routing.loader.yaml_collection service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

1x: The template "@Twig/exception.css.twig" is deprecated since Symfony 4.4, will be removed in 5.0. ("@Twig/exception.css.twig" at line 1). 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

1x: The template "@Twig/layout.html.twig" is deprecated since Symfony 4.4, will be removed in 5.0. ("@Twig/layout.html.twig" at line 1). 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

1x: The FOS\RestBundle\Controller\ExceptionController class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

1x: The "fos_rest.exception.twig_controller" service is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\RestXmlCollectionLoader class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Inflector\DoctrineInflector class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\RestYamlCollectionLoader class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\DirectoryRouteLoader class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\Reader\RestActionReader class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\Reader\RestControllerReader class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\RestRouteLoader class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The FOS\RestBundle\Routing\Loader\RestRouteProcessor class is deprecated since FOSRestBundle 2.8. 1x in PublicHolidayControllerTest::testListAll from App\Tests\Controller

1x: The template "@Twig/base_js.html.twig" is deprecated since Symfony 4.4, will be removed in 5.0. ("@Twig/base_js.html.twig" at line 1). 1x in PublicHolidayControllerTest::testListNotAuth from App\Tests\Controller

So i changed some config :

fos_rest:
  body_listener: true
  routing_loader: false
  exceptions:
    exception_listener: false
    serialize_exceptions: false

The only remaining is the one about the classify method (first one) but now the exceptions are not handled and the response is not more a JSON result like :

{
  "code": 404,
  "message": "Entity not found"
}

Since that change, all my functional tests with an exception are failing.

Any idea ?

Thank you

xabbuh commented 4 years ago

Can you check if https://github.com/FriendsOfSymfony/FOSRestBundle/issues/2200#issuecomment-629150462 works for you too?

vincentjdc commented 4 years ago

Hi @xabbuh ,

I read that issue before posting mine (always searching before posting ;-)). Sadly, it doesn't change anything :-s

Please also note that :

xabbuh commented 4 years ago

Is there a special reason for setting framework.error_controller to null?

vincentjdc commented 4 years ago

Is there a special reason for setting framework.error_controller to null?

Probably but sincerely I can't remember why I set it to null. It was when I upgraded the project to Symfony 4.4 and got a lot of deprecation warnings (in relation with exception management).

I just removed that setting and the behavior is different :

Note that I have fos_rest.execption.codes.Doctrine\ORM\EntityNotFoundException set to 404. I assume that error handling is done only by Symfony and so that conf is not used anymore ?

For the first point, I can replace all EntityNotFoundException by throwingSymfony\Component\HttpKernel\Exception\NotFoundHttpException. It will "solve" the problem.

For the second, it is blocking as result in not JSON.

Am I missing something else ?

xabbuh commented 4 years ago

@vincentjdc Did you manage to resolve the issues that you had?

fliespl commented 4 years ago

@vincentjdc I am also curious if you fixed since I am probably having same issue now.

ymotuz-sc commented 3 years ago

Hi! I also have trouble with exceptions, could you please share solutions? Environment:

  1. Symfony: 4.4.18
  2. friendsofsymfony/rest-bundle: 2.8.5
  3. jms/serializer-bundle: 3.5.0

Steps and configurations:

  1. Upgrade FOSRestBundle from 2.7 to 2.8
  2. Set configurations in the fos_rest.yaml file as shown below
    fos_rest:
    body_listener: true
    view:
    view_response_listener: true
    failed_validation: 422
    empty_content: 204
    formats:
      rss: false
      xml: false
      json: true
    serializer:
    serialize_null: true
    exception:
    enabled: true
    exception_listener: false
    serializer_error_renderer: true
    serialize_exceptions: false
    map_exception_codes: true
    messages:
      'Symfony\Component\HttpKernel\Exception\HttpException': true
    body_converter:
    enabled: true
    #access_denied_listener:
    #  json: true
    format_listener:
    rules:
      - { path: '^/api',  priorities: ['json'], fallback_format: json, prefer_extension: true }
      - { path: '^/', priorities: [ 'html', '*/*'], fallback_format: html, prefer_extension: true }
    routing_loader:
    enabled: false

    configuration before changes:

    exception:
    enabled: true
    messages:
      'Symfony\Component\HttpKernel\Exception\HttpException': true
    routing_loader:
    enabled: true
    access_denied_listener:
    json: true

Now, when app thrown an exception(for example Symfony\Component\HttpKernel\Exception\NotFoundHttpException), I get a response in HTML format instead of JSON

Should I implement my custom hander for exceptions via event listener or something else or add some additional configurations? Thanks!