Skip to content

Commit 26d700e

Browse files
authored
feat(symfony): add error page (#6389)
1 parent 56153b7 commit 26d700e

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

features/main/exception_to_status.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ Feature: Using exception_to_status config
4545
And I send a "GET" request to "/issue5924"
4646
Then the response status code should be 429
4747
Then the header "retry-after" should be equal to 32
48+
49+
Scenario: Show error page
50+
When I add "Accept" header equal to "text/html"
51+
And I send a "GET" request to "/errors/404"
52+
Then the response status code should be 200
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Symfony\Action;
15+
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19+
20+
final class ErrorPageAction
21+
{
22+
public function __invoke(Request $request): Response
23+
{
24+
$status = $request->attributes->get('status');
25+
$text = Response::$statusTexts[$status] ?? throw new NotFoundHttpException();
26+
27+
return new Response(<<<HTML
28+
<!DOCTYPE html>
29+
<html>
30+
<head>
31+
<meta charset="UTF-8" />
32+
<title>Error $status</title>
33+
</head>
34+
<body><h1>Error $status</h1>$text</body>
35+
</html>
36+
HTML);
37+
}
38+
}

src/Symfony/Bundle/Resources/config/api.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<service id="api_platform.property_accessor" alias="property_accessor" public="false" />
1414
<service id="api_platform.property_info" alias="property_info" public="false" />
1515
<service id="api_platform.negotiator" class="Negotiation\Negotiator" public="false" />
16+
<service id="api_platform.action.error_page" class="ApiPlatform\Symfony\Action\ErrorPageAction" public="true" />
1617

1718
<!-- deprecated -->
1819
<service id="ApiPlatform\Action\NotFoundAction" alias="api_platform.action.not_found" public="true" />

src/Symfony/Bundle/Resources/config/routing/errors.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
http://symfony.com/schema/routing/routing-1.0.xsd">
77

88
<route id="api_errors" path="/errors/{status}" methods="GET|HEAD">
9-
<default key="_controller">api_platform.action.not_exposed</default>
10-
<default key="status">500</default>
9+
<default key="_controller">api_platform.action.error_page</default>
1110

1211
<requirement key="status">\d+</requirement>
1312
</route>

0 commit comments

Comments
 (0)