Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4045a13

Browse files
committed
feature #31398 [TwigBundle] Deprecating error templates for non-html formats and using ErrorRenderer as fallback (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [TwigBundle] Deprecating error templates for non-html formats and using ErrorRenderer as fallback | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - In the previous [PR](#31065) we created a new mechanism to render any PHP error/exception in a formatted string, which if the FB is enabled, would return an HTTP Response according to the preferred Request format (html, json, xml, txt, etc.), but when installing the TwigBundle this rendering mechanism is replaced by the current ExceptionController. This ExceptionController allows us to render custom error pages based on Twig in many formats, just what is already supported with the new ErrorRenderer component, so let's deprecate this in favor of the native. Commits ------- bf0c24a Deprecating error templates for non-html formats and using ErrorRenderer
2 parents 379b525 + bf0c24a commit 4045a13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+404
-26
lines changed

UPGRADE-4.4.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,74 @@ TwigBridge
151151

152152
* Deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
153153
`DebugCommand::__construct()` method, swap the variables position.
154+
155+
TwigBundle
156+
----------
157+
158+
* Deprecated default value `twig.controller.exception::showAction` of the `twig.exception_controller` configuration option,
159+
set it to `null` instead. This will also change the default error response format according to https://tools.ietf.org/html/rfc7807
160+
for `json`, `xml`, `atom` and `txt` formats:
161+
162+
Before:
163+
```json
164+
{
165+
"error": {
166+
"code": 404,
167+
"message": "Sorry, the page you are looking for could not be found"
168+
}
169+
}
170+
```
171+
172+
After:
173+
```json
174+
{
175+
"title": "Not Found",
176+
"status": 404,
177+
"detail": "Sorry, the page you are looking for could not be found"
178+
}
179+
```
180+
181+
* Deprecated the `ExceptionController` and all built-in error templates, use the error renderer mechanism of the `ErrorRenderer` component
182+
* Deprecated loading custom error templates in non-html formats. Custom HTML error pages based on Twig keep working as before:
183+
184+
Before (`templates/bundles/TwigBundle/Exception/error.jsonld.twig`):
185+
```twig
186+
{
187+
"@id": "https://example.com",
188+
"@type": "error",
189+
"@context": {
190+
"title": "{{ status_text }}",
191+
"code": {{ status_code }},
192+
"message": "{{ exception.message }}"
193+
}
194+
}
195+
```
196+
197+
After (`App\ErrorRenderer\JsonLdErrorRenderer`):
198+
```php
199+
class JsonLdErrorRenderer implements ErrorRendererInterface
200+
{
201+
public static function getFormat(): string
202+
{
203+
return 'jsonld';
204+
}
205+
206+
public function render(FlattenException $exception): string
207+
{
208+
return json_encode([
209+
'@id' => 'https://example.com',
210+
'@type' => 'error',
211+
'@context' => [
212+
'title' => $exception->getTitle(),
213+
'code' => $exception->getStatusCode(),
214+
'message' => $exception->getMessage(),
215+
],
216+
]);
217+
}
218+
}
219+
```
220+
221+
Configure your rendering service tagging it with `error_renderer.renderer`.
154222

155223
Validator
156224
---------

UPGRADE-5.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ TwigBundle
467467
* The default value (`false`) of the `twig.strict_variables` configuration option has been changed to `%kernel.debug%`.
468468
* The `transchoice` tag and filter have been removed, use the `trans` ones instead with a `%count%` parameter.
469469
* Removed support for legacy templates directories `src/Resources/views/` and `src/Resources/<BundleName>/views/`, use `templates/` and `templates/bundles/<BundleName>/` instead.
470+
* The default value (`twig.controller.exception::showAction`) of the `twig.exception_controller` configuration option has been changed to `null`.
471+
* Removed `ExceptionController` class and all built-in error templates
470472

471473
TwigBridge
472474
----------

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Fragment/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ framework:
77

88
twig:
99
strict_variables: '%kernel.debug%'
10+
exception_controller: ~

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"symfony/stopwatch": "^3.4|^4.0|^5.0",
5353
"symfony/translation": "^4.3|^5.0",
5454
"symfony/templating": "^3.4|^4.0|^5.0",
55-
"symfony/twig-bundle": "^3.4|^4.0|^5.0",
55+
"symfony/twig-bundle": "^4.4|^5.0",
5656
"symfony/validator": "^4.1|^5.0",
5757
"symfony/var-dumper": "^4.3|^5.0",
5858
"symfony/workflow": "^4.3|^5.0",
@@ -80,6 +80,7 @@
8080
"symfony/stopwatch": "<3.4",
8181
"symfony/translation": "<4.3",
8282
"symfony/twig-bridge": "<4.1.1",
83+
"symfony/twig-bundle": "<4.4",
8384
"symfony/validator": "<4.1",
8485
"symfony/workflow": "<4.3"
8586
},

src/Symfony/Bundle/SecurityBundle/Tests/Functional/JsonLoginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public function testDefaultJsonLoginBadRequest()
7070

7171
$this->assertSame(400, $response->getStatusCode());
7272
$this->assertSame('application/json', $response->headers->get('Content-Type'));
73-
$this->assertArraySubset(['error' => ['code' => 400, 'message' => 'Bad Request']], json_decode($response->getContent(), true));
73+
$this->assertArraySubset(['title' => 'Bad Request', 'status' => 400, 'detail' => 'Invalid JSON.'], json_decode($response->getContent(), true));
7474
}
7575
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\app;
13+
14+
use Symfony\Component\ErrorRenderer\ErrorRenderer;
15+
use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer;
16+
use Symfony\Component\ErrorRenderer\ErrorRenderer\JsonErrorRenderer;
17+
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
21+
class ExceptionController
22+
{
23+
private $errorRenderer;
24+
25+
public function __construct()
26+
{
27+
$this->errorRenderer = new ErrorRenderer([
28+
new HtmlErrorRenderer(),
29+
new JsonErrorRenderer(),
30+
]);
31+
}
32+
33+
public function __invoke(Request $request, FlattenException $exception)
34+
{
35+
return new Response($this->errorRenderer->render($exception, $request->getPreferredFormat()), $exception->getStatusCode());
36+
}
37+
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/JsonLoginLdap/bundles.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@
1212
return [
1313
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
1414
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
15-
new Symfony\Bundle\TwigBundle\TwigBundle(),
1615
];

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/JsonLoginLdap/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
imports:
2-
- { resource: ./../config/default.yml }
2+
- { resource: ./../config/framework.yml }
33
services:
44
Symfony\Component\Ldap\Ldap:
55
arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/SecurityHelper/bundles.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1313
use Symfony\Bundle\SecurityBundle\SecurityBundle;
14-
use Symfony\Bundle\TwigBundle\TwigBundle;
1514

1615
return [
1716
new FrameworkBundle(),
1817
new SecurityBundle(),
19-
new TwigBundle(),
2018
];

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/SecurityHelper/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
imports:
2-
- { resource: ./../config/default.yml }
2+
- { resource: ./../config/framework.yml }
33

44
services:
55
# alias the service so we can access it in the tests

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/twig.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
twig:
33
debug: '%kernel.debug%'
44
strict_variables: '%kernel.debug%'
5+
exception_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\app\ExceptionController

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-xml": "*",
2121
"symfony/config": "^4.2|^5.0",
2222
"symfony/dependency-injection": "^4.2|^5.0",
23-
"symfony/http-kernel": "^4.3",
23+
"symfony/http-kernel": "^4.4",
2424
"symfony/security-core": "^4.3",
2525
"symfony/security-csrf": "^4.2|^5.0",
2626
"symfony/security-guard": "^4.2|^5.0",
@@ -33,10 +33,10 @@
3333
"symfony/css-selector": "^3.4|^4.0|^5.0",
3434
"symfony/dom-crawler": "^3.4|^4.0|^5.0",
3535
"symfony/form": "^3.4|^4.0|^5.0",
36-
"symfony/framework-bundle": "^4.2|^5.0",
36+
"symfony/framework-bundle": "^4.4|^5.0",
3737
"symfony/http-foundation": "^3.4|^4.0|^5.0",
3838
"symfony/translation": "^3.4|^4.0|^5.0",
39-
"symfony/twig-bundle": "^4.2|^5.0",
39+
"symfony/twig-bundle": "^4.4|^5.0",
4040
"symfony/twig-bridge": "^3.4|^4.0|^5.0",
4141
"symfony/process": "^3.4|^4.0|^5.0",
4242
"symfony/validator": "^3.4|^4.0|^5.0",
@@ -48,9 +48,9 @@
4848
},
4949
"conflict": {
5050
"symfony/browser-kit": "<4.2",
51-
"symfony/twig-bundle": "<4.2",
51+
"symfony/twig-bundle": "<4.4",
5252
"symfony/var-dumper": "<3.4",
53-
"symfony/framework-bundle": "<4.2",
53+
"symfony/framework-bundle": "<4.4",
5454
"symfony/console": "<3.4"
5555
},
5656
"autoload": {

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ CHANGELOG
66

77
* marked the `TemplateIterator` as `internal`
88
* added HTML comment to beginning and end of `exception_full.html.twig`
9+
* added a new `TwigHtmlErrorRenderer` for `html` format, integrated with the `ErrorRenderer` component
10+
* deprecated `ExceptionController` class and all built-in error templates in favor of the new error renderer mechanism
11+
* deprecated default value `twig.controller.exception::showAction` of `twig.exception_controller` configuration option, set it to `null` instead
912

1013
4.2.0
1114
-----

src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
use Twig\Error\LoaderError;
2020
use Twig\Loader\ExistsLoaderInterface;
2121

22+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use the ErrorRenderer component instead.', ExceptionController::class), E_USER_DEPRECATED);
23+
2224
/**
2325
* ExceptionController renders error or exception pages for a given
2426
* FlattenException.
2527
*
2628
* @author Fabien Potencier <[email protected]>
2729
* @author Matthias Pigulla <[email protected]>
30+
*
31+
* @deprecated since Symfony 4.4, use the ErrorRenderer component instead.
2832
*/
2933
class ExceptionController
3034
{

src/Symfony/Bundle/TwigBundle/Controller/PreviewErrorController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\Controller;
1313

14+
use Symfony\Component\ErrorRenderer\ErrorRenderer;
1415
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
1516
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
1618
use Symfony\Component\HttpKernel\HttpKernelInterface;
1719

1820
/**
@@ -26,16 +28,22 @@ class PreviewErrorController
2628
{
2729
protected $kernel;
2830
protected $controller;
31+
private $errorRenderer;
2932

30-
public function __construct(HttpKernelInterface $kernel, $controller)
33+
public function __construct(HttpKernelInterface $kernel, $controller, ErrorRenderer $errorRenderer = null)
3134
{
3235
$this->kernel = $kernel;
3336
$this->controller = $controller;
37+
$this->errorRenderer = $errorRenderer;
3438
}
3539

3640
public function previewErrorPageAction(Request $request, $code)
3741
{
38-
$exception = FlattenException::createFromThrowable(new \Exception('Something has intentionally gone wrong.'), $code);
42+
$exception = FlattenException::createFromThrowable(new \Exception('Something has intentionally gone wrong.'), $code, ['X-Debug' => false]);
43+
44+
if (null === $this->controller && null !== $this->errorRenderer) {
45+
return new Response($this->errorRenderer->render($exception, $request->getPreferredFormat()), $code);
46+
}
3947

4048
/*
4149
* This Request mimics the parameters set by

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ public function getConfigTreeBuilder()
3434

3535
$rootNode
3636
->children()
37-
->scalarNode('exception_controller')->defaultValue('twig.controller.exception::showAction')->end()
37+
->scalarNode('exception_controller')
38+
->defaultValue(static function () {
39+
@trigger_error('Relying on the default value ("twig.controller.exception::showAction") of the "twig.exception_controller" configuration option is deprecated since Symfony 4.4, set it to "null" explicitly instead, which will be the new default in 5.0.', E_USER_DEPRECATED);
40+
41+
return 'twig.controller.exception::showAction';
42+
})
43+
->end()
3844
->end()
3945
;
4046

0 commit comments

Comments
 (0)