Skip to content

Commit

Permalink
feat(symfony): add error page (#6389)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Sep 11, 2024
1 parent 56153b7 commit 26d700e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions features/main/exception_to_status.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ Feature: Using exception_to_status config
And I send a "GET" request to "/issue5924"
Then the response status code should be 429
Then the header "retry-after" should be equal to 32

Scenario: Show error page
When I add "Accept" header equal to "text/html"
And I send a "GET" request to "/errors/404"
Then the response status code should be 200
38 changes: 38 additions & 0 deletions src/Symfony/Action/ErrorPageAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Symfony\Action;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class ErrorPageAction
{
public function __invoke(Request $request): Response
{
$status = $request->attributes->get('status');
$text = Response::$statusTexts[$status] ?? throw new NotFoundHttpException();

return new Response(<<<HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Error $status</title>
</head>
<body><h1>Error $status</h1>$text</body>
</html>
HTML);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<service id="api_platform.property_accessor" alias="property_accessor" public="false" />
<service id="api_platform.property_info" alias="property_info" public="false" />
<service id="api_platform.negotiator" class="Negotiation\Negotiator" public="false" />
<service id="api_platform.action.error_page" class="ApiPlatform\Symfony\Action\ErrorPageAction" public="true" />

<!-- deprecated -->
<service id="ApiPlatform\Action\NotFoundAction" alias="api_platform.action.not_found" public="true" />
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Bundle/Resources/config/routing/errors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
http://symfony.com/schema/routing/routing-1.0.xsd">

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

<requirement key="status">\d+</requirement>
</route>
Expand Down

0 comments on commit 26d700e

Please sign in to comment.