8000 [HttpKernel] ErrorListener doesnt convert throwable to legacy FlattenException for controller argument · Issue #34661 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] ErrorListener doesnt convert throwable to legacy FlattenException for controller argument #34661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrew-demb opened this issue Nov 27, 2019 · 4 comments

Comments

@andrew-demb
Copy link
Contributor
andrew-demb commented Nov 27, 2019

Symfony version(s) affected: 4.4.0

Description

ErrorListener doesnt convert throwable to legacy FlattenException for controller argument and it leads to exception:

Argument 1 passed to ExceptionController::__invoke() must be an instance of Symfony\Component\Debug\Exception\FlattenException, instance of TypeError given, called in /.../vendor/symfony/http-kernel/HttpKernel.php on line 146

How to reproduce

Declare controller with Symfony\Component\Debug\Exception\FlattenException argument,
use Symfony\Component\HttpKernel\EventListener\ErrorListener with configured reference to controller.

Possible Solution

Check Symfony\Component\Debug\Exception\FlattenException here

if ($r && (!$r->hasType() || FlattenException::class === $r->getType()->getName())) {

Additional context

DI config:

services:
  exception_error.listener:
  class: Symfony\Component\HttpKernel\EventListener\ExceptionListener
  tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: -1 }
  arguments:
    - 'ExceptionController'
    - '@?logger'

Controller example:

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\JsonResponse;

class ExceptionController
{
    public function __invoke(FlattenException $exception): JsonResponse
    {
        return new JsonResponse(['message' => $exception->getMessage(), 'failed' => true]);
    }
}
andrew-demb added a commit to andrew-demb/symfony that referenced this issue Nov 27, 2019
…ebug\Exception\FlattenException` in controller
@andrew-demb
Copy link
Contributor Author

Temporary workaround for me (in case, that I should preserve BC with ^3.4|^4.0 versions) - drop typehint in controller argument.

@yceruto
Copy link
Member
yceruto commented Nov 27, 2019

I'm confused here, your DI config refers to ExceptionListener using Debug\FlattenException, but your previous description refers to ErrorListener. So if you have changed to ErrorListener why not change to ErrorHandler\FlattenException in your controller?

@andrew-demb
Copy link
Contributor Author

Sorry for misleading DI config example.
Listener class should be ErrorListener in DI.

I didn't change typehint in controller due to reason, that controller located in bundle, that supports ^3.4|^4.0 and ErrorHandler\FlattenException introduced only in 4.4.

Listener I replaced via DI extension on build (after check for current symfony version >=4.4).

@andrew-demb
Copy link
Contributor Author

I see, that it's hard for me to explain my implementation.

So, I created gist.
https://gist.github.com/andrew-demb/11a4a3ddff33523c5cb62c2f55586e49

nicolas-grekas added a commit that referenced this issue Nov 28, 2019
…on in controller (andrew-demb)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpKernel] Support typehint to deprecated FlattenException in controller

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34661
| License       | MIT
| Doc PR        |

We should support converting throwable to `FlattenException` with typehint to deprecated class also

Commits
-------

26b4e37 [HttpKernel] Support typehint to deprecated FlattenException in controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0