Skip to content
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
7 changes: 7 additions & 0 deletions features/main/exception_to_status.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ Feature: Using exception_to_status config
And I send a "DELETE" request to "/error_with_overriden_status/1"
Then the response status code should be 403
And the JSON node "status" should be equal to 403

@!mongodb
Scenario: Get HTTP Exception headers
When I add "Accept" header equal to "application/ld+json"
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
7 changes: 7 additions & 0 deletions src/State/Processor/RespondProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\State\Processor;

use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation;
Expand All @@ -24,6 +25,7 @@
use ApiPlatform\Metadata\Util\CloneTrait;
use ApiPlatform\State\ProcessorInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;

/**
* Serializes data.
Expand Down Expand Up @@ -64,6 +66,11 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
'X-Frame-Options' => 'deny',
];

$exception = $request->attributes->get('exception');
if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not a huge fan of this right now because of the HttpKernel dependency, I'll see to probably move this in the bridge in the future.

$headers = array_merge($headers, $exceptionHeaders);
}

$status = $operation->getStatus();

if ($sunset = $operation->getSunset()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\Issue5924;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

#[Get(uriTemplate: 'issue5924{._format}', read: true, provider: [TooManyRequests::class, 'provide'])]
class TooManyRequests
{
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): void
{
throw new TooManyRequestsHttpException(32);
}
}
16 changes: 16 additions & 0 deletions tests/State/RespondProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

class RespondProcessorTest extends TestCase
{
Expand Down Expand Up @@ -97,4 +98,19 @@ public function testRedirectToOperation(): void
$this->assertSame(200, $response->getStatusCode());
$this->assertNull($response->headers->get('Location'));
}

public function testAddsExceptionHeaders(): void
{
$operation = new Get();

/** @var ProcessorInterface<Response> $respondProcessor */
$respondProcessor = new RespondProcessor();
$req = new Request();
$req->attributes->set('exception', new TooManyRequestsHttpException(32));
$response = $respondProcessor->process('content', new Get(), context: [
'request' => $req,
]);

$this->assertSame('32', $response->headers->get('retry-after'));
}
}