FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

How to handle exception in 2.8 ?

Open vincentjdc opened this issue 4 years ago • 7 comments

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

vincentjdc avatar May 27 '20 16:05 vincentjdc

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

xabbuh avatar May 27 '20 17:05 xabbuh

Hi @xabbuh ,

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

Please also note that :

  • I don't have any custom JMS handler
  • I have that conf for twig :
twig:
    # ...
    exception_controller: null
  • I have that conf for framework :
framework:
    # ...
    error_controller: null

vincentjdc avatar May 28 '20 11:05 vincentjdc

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

xabbuh avatar May 28 '20 12:05 xabbuh

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 :

  • Throwing Doctrine\ORM\EntityNotFoundException leads to a 500 error (in JSON)

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 ?

  • denyAccessUnlessGranted in Controller leads to the 403 error (debug page), so not in JSON.

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 ?

vincentjdc avatar May 28 '20 13:05 vincentjdc

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

xabbuh avatar Jun 29 '20 12:06 xabbuh

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

fliespl avatar Sep 03 '20 08:09 fliespl

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!

ymotuz-sc avatar Feb 02 '21 15:02 ymotuz-sc