Skip to content

Commit

Permalink
Add HttpErrorException (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik authored Aug 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 33bd2a0 commit 4e6e8b1
Showing 3 changed files with 43 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Driver/Internal/AbstractHttpDriver.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\Driver\HttpDriver;
use Amp\Http\Server\ErrorHandler;
use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
@@ -53,6 +54,8 @@ final protected function handleRequest(Request $request): void
$response = $this->requestHandler->handleRequest($request);
} catch (ClientException $exception) {
throw $exception;
} catch (HttpErrorException $exception) {
$response = $this->handleError($exception->getStatus(), $exception->getReason(), $request);
} catch (\Throwable $exception) {
$response = $this->handleInternalServerError($request, $exception);
} finally {
@@ -118,8 +121,13 @@ private function handleInternalServerError(Request $request, \Throwable $excepti
],
);

return $this->handleError($status, null, $request);
}

private function handleError(int $status, ?string $reason, Request $request): Response
{
try {
return $this->errorHandler->handleError($status, null, $request);
return $this->errorHandler->handleError($status, $reason, $request);
} catch (\Throwable $exception) {
// If the error handler throws, fallback to returning the default error page.
$this->logger->error(
21 changes: 21 additions & 0 deletions src/HttpErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Amp\Http\Server;

final class HttpErrorException extends \Exception
{
public function __construct(private readonly int $status, private readonly ?string $reason = null)
{
parent::__construct('Error ' . $status . ($this->reason !== null && $this->reason !== '' ? ': ' . $reason : ''));
}

public function getReason(): ?string
{
return $this->reason;
}

public function getStatus(): int
{
return $this->status;
}
}
13 changes: 13 additions & 0 deletions test/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
use Amp\Http\HttpStatus;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\ErrorHandler;
use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
@@ -143,4 +144,16 @@ public function testError(): void

self::assertSame(HttpStatus::INTERNAL_SERVER_ERROR, $response->getStatus());
}

public function testHttpError(): void
{
$this->httpServer->start(new ClosureRequestHandler(function (Request $req) {
throw new HttpErrorException(401, 'test');
}), new DefaultErrorHandler());

$response = $this->httpClient->request(new ClientRequest($this->getAuthority() . "/foo"));

self::assertSame(401, $response->getStatus());
self::assertSame('test', $response->getReason());
}
}

0 comments on commit 4e6e8b1

Please sign in to comment.