Skip to content

Keeping backward compatibility with legacy FlattenException usage #33941

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

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ HttpKernel
current directory or with a glob pattern. The fallback directories have never been advocated
so you likely do not use those in any app based on the SF Standard or Flex edition.
* Getting the container from a non-booted kernel is deprecated
* Deprecated passing the `exception` attribute (instance of `Symfony\Component\Debug\Exception\FlattenException`)
to the configured controller of the `ExceptionListener`, use the `e` attribute
(instance of `Symfony\Component\ErrorRenderer\Exception\FlattenException`) instead

before:
```php
use Symfony\Component\Debug\Exception\FlattenException;

class ExceptionController
{
public function __invoke(FlattenException $exception)
{
}
}
```

after:
```php
use Symfony\Component\ErrorRenderer\Exception\FlattenException;

class ExceptionController
{
public function __invoke(FlattenException $e)
{
}
}
```

Lock
----
Expand Down
27 changes: 27 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,33 @@ HttpKernel
* Removed the second and third argument of `FileLocator::__construct`
* Removed loading resources from `%kernel.root_dir%/Resources` and `%kernel.root_dir%` as
fallback directories.
* Removed passing the `exception` attribute (instance of `Symfony\Component\Debug\Exception\FlattenException`)
to the configured controller of the `ExceptionListener`, use the `e` attribute
(instance of `Symfony\Component\ErrorRenderer\Exception\FlattenException`) instead

before:
```php
use Symfony\Component\Debug\Exception\FlattenException;

class ExceptionController
{
public function __invoke(FlattenException $exception)
{
}
}
```

after:
```php
use Symfony\Component\ErrorRenderer\Exception\FlattenException;

class ExceptionController
{
public function __invoke(FlattenException $e)
{
}
}
```

Intl
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Controller;

use Symfony\Component\ErrorRenderer\Exception\FlattenException;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Controller;

use Symfony\Component\ErrorRenderer\Exception\FlattenException;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Bundle\TwigBundle\Controller\PreviewErrorController;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down
22 changes: 7 additions & 15 deletions src/Symfony/Component/ErrorRenderer/Exception/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* Basically, this class removes all objects from the trace.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
class FlattenException
{
Expand All @@ -37,6 +39,11 @@ class FlattenException
private $file;
private $line;

public static function create(\Exception $exception, int $statusCode = null, array $headers = []): self
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restoring for a smooth migration, it can be deprecated in 5.1.

{
return static::createFromThrowable($exception, $statusCode, $headers);
}

public static function createFromThrowable(\Throwable $exception, int $statusCode = null, array $headers = []): self
{
$e = new static();
Expand Down Expand Up @@ -374,18 +381,3 @@ public function getAsString()
return rtrim($message);
}
}

namespace Symfony\Component\Debug\Exception;

if (!class_exists(FlattenException::class, false)) {
class_alias(\Symfony\Component\ErrorRenderer\Exception\FlattenException::class, FlattenException::class);
}

if (false) {
/**
* @deprecated since Symfony 4.4, use Symfony\Component\ErrorRenderer\Exception\FlattenException instead.
*/
class FlattenException extends \Symfony\Component\ErrorRenderer\Exception\FlattenException
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Debug\Exception;

if (!class_exists(FlattenException::class, false)) {
class_alias(\Symfony\Component\ErrorRenderer\Exception\FlattenException::class, FlattenException::class);
}

if (false) {
/**
* @deprecated since Symfony 4.4, use Symfony\Component\ErrorRenderer\Exception\FlattenException instead.
*/
class FlattenException extends \Symfony\Component\ErrorRenderer\Exception\FlattenException
{
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/ErrorRenderer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"autoload": {
"psr-4": { "Symfony\\Component\\ErrorRenderer\\": "" },
"classmap": [ "Resources/stubs/Exception/FlattenException.php" ],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would fix the compiler error mentioned here #33929 (comment)

"exclude-from-classmap": [
"/Tests/"
]
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ CHANGELOG
* Marked all dispatched event classes as `@final`
* Added `ErrorController` to enable the preview and error rendering mechanism
* Getting the container from a non-booted kernel is deprecated.
* Deprecated passing the `exception` attribute (instance of `Symfony\Component\Debug\Exception\FlattenException`)
to the configured controller of the `ExceptionListener`

4.3.0
-----
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/HttpKernel/Controller/ErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public function __construct(HttpKernelInterface $kernel, $controller, ErrorRende
$this->errorRenderer = $errorRenderer;
}

public function __invoke(Request $request, FlattenException $exception): Response
public function __invoke(Request $request, FlattenException $e): Response
{
try {
return new Response($this->errorRenderer->render($exception, $request->getPreferredFormat()), $exception->getStatusCode(), $exception->getHeaders());
} catch (ErrorRendererNotFoundException $e) {
return new Response($this->errorRenderer->render($exception), $exception->getStatusCode(), $exception->getHeaders());
return new Response($this->errorRenderer->render($e, $request->getPreferredFormat()), $e->getStatusCode(), $e->getHeaders());
} catch (ErrorRendererNotFoundException $_) {
return new Response($this->errorRenderer->render($e), $e->getStatusCode(), $e->getHeaders());
}
}

Expand All @@ -57,7 +57,7 @@ public function preview(Request $request, int $code): Response
*/
$subRequest = $request->duplicate(null, null, [
'_controller' => $this->controller,
'exception' => $exception,
'e' => $exception,
'logger' => null,
'showException' => false,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -123,9 +124,21 @@ protected function logException(\Exception $exception, $message)
*/
protected function duplicateRequest(\Exception $exception, Request $request)
{
@trigger_error(sprintf('Passing the "exception" attribute (instance of "%s") to the configured controller of the "%s" class is deprecated since Symfony 4.4, use the passed "e" attribute (instance of "%s") instead.', LegacyFlattenException::class, self::class, FlattenException::class));

$flattenException = FlattenException::createFromThrowable($exception);

// BC layer to be removed in 5.0
if (class_exists(\Symfony\Component\Debug\Debug::class, false)) {
$legacyFlattenException = LegacyFlattenException::createFromThrowable($exception);
} else {
$legacyFlattenException = $flattenException;
}

$attributes = [
'_controller' => $this->controller,
'exception' => FlattenException::createFromThrowable($exception),
'exception' => $legacyFlattenException, // to be removed in 5.0
'e' => $flattenException,
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
];
$request = $request->duplicate(null, null, $attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testPreviewController()
->method('handle')
->with(
$this->callback(function (Request $request) use ($_controller, $code) {
$exception = $request->attributes->get('exception');
$exception = $request->attributes->get('e');

$this->assertSame($_controller, $request->attributes->get('_controller'));
$this->assertInstanceOf(FlattenException::class, $exception);
Expand Down