Skip to content

Commit

Permalink
Fix(publish): Return 504 instead of 503 when a timeout occurs (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek authored Mar 19, 2024
1 parent c7d78b6 commit 36f4e00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Hub/Controller/PublishController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Freddie\Hub\Controller;

use Fig\Http\Message\StatusCodeInterface;
use Freddie\Helper\FlatQueryParser;
use Freddie\Hub\HubControllerInterface;
use Freddie\Hub\HubInterface;
Expand All @@ -13,8 +14,10 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;
use React\Promise\Timer\TimeoutException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\Uid\Ulid;
use Throwable;
Expand Down Expand Up @@ -74,6 +77,8 @@ public function __invoke(ServerRequestInterface $request): ResponseInterface

try {
await($this->hub->publish($update));
} catch (TimeoutException) {
throw new HttpException(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT);
} catch (Throwable) {
throw new ServiceUnavailableHttpException();
}
Expand Down
23 changes: 19 additions & 4 deletions tests/Unit/Hub/Controller/PublishControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
use React\Http\Message\Response;
use React\Http\Message\ServerRequest;
use React\Promise\PromiseInterface;
use React\Promise\Timer\TimeoutException;
use ReflectionClass;
use RuntimeException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Uid\Ulid;
use Throwable;

use function Freddie\Tests\create_jwt;
use function Freddie\Tests\handle;
Expand Down Expand Up @@ -210,11 +212,15 @@
'Your rights are not sufficient to publish this update.'
);

it('throws a service unavailable exception when publishing fails', function () {
$transport = new class implements TransportInterface {
it('complains when publishing fails', function (Throwable $exception, int $expectedStatusCode) {
$transport = new class ($exception) implements TransportInterface {
public function __construct(private Throwable $exception)
{
}

public function publish(Update $update): PromiseInterface
{
return reject(new RuntimeException('☠️'));
return reject($this->exception);
}

public function subscribe(callable $callback): void
Expand Down Expand Up @@ -257,6 +263,15 @@ public function reconciliate(string $lastEventID): Generator
$response = handle($app, $request);

// Then
expect($response->getStatusCode())->toBe(StatusCodeInterface::STATUS_SERVICE_UNAVAILABLE)
expect($response->getStatusCode())->toBe($expectedStatusCode)
->and((string) $response->getBody())->toBeEmpty();
})->with(function () {
yield 'general error' => [
'exception' => new RuntimeException('☠️'),
'expectedStatusCode' => StatusCodeInterface::STATUS_SERVICE_UNAVAILABLE,
];
yield 'timeout' => [
'exception' => new TimeoutException(0),
'expectedStatusCode' => StatusCodeInterface::STATUS_GATEWAY_TIMEOUT,
];
});

0 comments on commit 36f4e00

Please sign in to comment.