Fosrestbundle: Still getting "getStatusCode" Exception with Symfony 4.4 & FOSRestBundle 2.7

Created on 12 Feb 2020  路  9Comments  路  Source: FriendsOfSymfony/FOSRestBundle

Problem

In some cases (when I have a PHP syntax error), Symfony shows a 500 Exception with the message:

Argument 1 passed to FOS\RestBundle\Controller\ExceptionController::getStatusCode() must be an instance of Exception, instance of ParseError given, called in /var/www/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php on line 68

The problem is present with Symfony 4.4 (freshly installed with `symfony new app --version=4.4) and FOSRestBundle 2.7

To reproduce

The following example is pretty simple : 1 table user with 1 field firstName and a controller to list the users.

  1. Pull the repository https://github.com/vincentjdc/fosrest-status-code-exception-example
  2. Configure a database + update the .env file to set up the database
  3. run composer install to install vendors
  4. run bin/console doctrine:migrations:migrate to create tables
  5. run bin/console doctrine:fixtures:load to insert a user in the user table
  6. Go to the /api route, you should see a list with 1 user "Vincent"
  7. Insert an error in src/Entity/User.php (by removing a semicolon for example)
  8. Refresh the /api page, you shoud see the exception.

Expected

An error message with the real problem (semicolon)

Bug

Most helpful comment

The bug isn麓t resolved. Now it hangs on the very next line:
Uncaught Error: Argument 3 passed to FOS\RestBundle\Controller\ExceptionController::getTemplateData() must be an instance of Exception, instance of Error given, called in /var/www/html/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php on line 74
Please fix the call for the function "createView", too.

All 9 comments

I have the same problem FYI

I open pr to solve this : #2093

@GuigZ- I can't get a functional test to handle a ParseError. I tried both in the test controller and in a kernel response subscriber, trying to get the parse error to happen later in the process. Patch is here.

@GuigZ- I can't get a functional test to handle a ParseError. I tried both in the test controller and in a kernel response subscriber, trying to get the parse error to happen later in the process. Patch is here.

With which php version?

Edit: the case is not that, the error is triggered in controller not in subscriber

With which php version?

PHP 7.2.27-6+ubuntu18.04.1+deb.sury.org+1

Edit: the case is not that, the error is triggered in controller not in subscriber

Happens the same in the controller action. Making it happen after the kernel response event is dispatched was just a shot in the dark. ParseError is thrown and not handled with this as well:

public function nonexceptionAction()
{
    eval('echo "This is a" echo "ParseError"');

    return new Response('No response expected');
}

@iisisrael i create a same tests with your code and i trigger two errors :

1) FOS\RestBundle\Tests\Functional\ExceptionControllerTest::testExceptionControllerHandlesException
Error: Call to undefined method FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\ThrowableController::fooBar()

/Users/guigz/Workspace/FOSRestBundle/Tests/Functional/Bundle/TestBundle/Controller/ThrowableController.php:20
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/HttpKernel.php:145
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/HttpKernel.php:67
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/Kernel.php:191
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/HttpKernelBrowser.php:63
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/framework-bundle/KernelBrowser.php:131
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/browser-kit/AbstractBrowser.php:390
/Users/guigz/Workspace/FOSRestBundle/Tests/Functional/ExceptionControllerTest.php:32

2) FOS\RestBundle\Tests\Functional\ExceptionControllerTest::testExceptionControllerHandlesNonexceptionThrowable
ParseError: syntax error, unexpected 'echo' (T_ECHO), expecting ';' or ','

/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/HttpKernel.php:145
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/HttpKernel.php:67
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/Kernel.php:191
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/http-kernel/HttpKernelBrowser.php:63
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/framework-bundle/KernelBrowser.php:131
/Users/guigz/Workspace/FOSRestBundle/vendor/symfony/browser-kit/AbstractBrowser.php:390
/Users/guigz/Workspace/FOSRestBundle/Tests/Functional/ExceptionControllerTest.php:42

My controller is just that :

<?php

/*
 * This file is part of the FOSRestBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class ThrowableController
{
    public function exceptionAction()
    {
        $this->fooBar();

        return new Response('No response expected');
    }

    public function nonexceptionAction()
    {
        eval("echo 'a' echo 'b'");
        return new Response('No response expected');
    }
}

@iisisrael you can not reproduce this bug with phpunit ... because phpunit use a custom error handler :)

fixed in #2093

The bug isn麓t resolved. Now it hangs on the very next line:
Uncaught Error: Argument 3 passed to FOS\RestBundle\Controller\ExceptionController::getTemplateData() must be an instance of Exception, instance of Error given, called in /var/www/html/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php on line 74
Please fix the call for the function "createView", too.

Was this page helpful?
0 / 5 - 0 ratings