Skip to content

Commit b95087c

Browse files
committed
Deprecating error templates for non-html formats and using ErrorRenderer
1 parent a7852c0 commit b95087c

Some content is hidden

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

59 files changed

+406
-27
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 keeps 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 <fabien@symfony.com>
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

0 commit comments

Comments
 (0)