Skip to content

Commit 4e6e8b1

Browse files
authored
Add HttpErrorException (#352)
1 parent 33bd2a0 commit 4e6e8b1

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Driver/Internal/AbstractHttpDriver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Amp\Http\Server\DefaultErrorHandler;
88
use Amp\Http\Server\Driver\HttpDriver;
99
use Amp\Http\Server\ErrorHandler;
10+
use Amp\Http\Server\HttpErrorException;
1011
use Amp\Http\Server\Request;
1112
use Amp\Http\Server\RequestHandler;
1213
use Amp\Http\Server\Response;
@@ -53,6 +54,8 @@ final protected function handleRequest(Request $request): void
5354
$response = $this->requestHandler->handleRequest($request);
5455
} catch (ClientException $exception) {
5556
throw $exception;
57+
} catch (HttpErrorException $exception) {
58+
$response = $this->handleError($exception->getStatus(), $exception->getReason(), $request);
5659
} catch (\Throwable $exception) {
5760
$response = $this->handleInternalServerError($request, $exception);
5861
} finally {
@@ -118,8 +121,13 @@ private function handleInternalServerError(Request $request, \Throwable $excepti
118121
],
119122
);
120123

124+
return $this->handleError($status, null, $request);
125+
}
126+
127+
private function handleError(int $status, ?string $reason, Request $request): Response
128+
{
121129
try {
122-
return $this->errorHandler->handleError($status, null, $request);
130+
return $this->errorHandler->handleError($status, $reason, $request);
123131
} catch (\Throwable $exception) {
124132
// If the error handler throws, fallback to returning the default error page.
125133
$this->logger->error(

src/HttpErrorException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Http\Server;
4+
5+
final class HttpErrorException extends \Exception
6+
{
7+
public function __construct(private readonly int $status, private readonly ?string $reason = null)
8+
{
9+
parent::__construct('Error ' . $status . ($this->reason !== null && $this->reason !== '' ? ': ' . $reason : ''));
10+
}
11+
12+
public function getReason(): ?string
13+
{
14+
return $this->reason;
15+
}
16+
17+
public function getStatus(): int
18+
{
19+
return $this->status;
20+
}
21+
}

test/IntegrationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Amp\Http\HttpStatus;
1111
use Amp\Http\Server\DefaultErrorHandler;
1212
use Amp\Http\Server\ErrorHandler;
13+
use Amp\Http\Server\HttpErrorException;
1314
use Amp\Http\Server\Request;
1415
use Amp\Http\Server\RequestHandler;
1516
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
@@ -143,4 +144,16 @@ public function testError(): void
143144

144145
self::assertSame(HttpStatus::INTERNAL_SERVER_ERROR, $response->getStatus());
145146
}
147+
148+
public function testHttpError(): void
149+
{
150+
$this->httpServer->start(new ClosureRequestHandler(function (Request $req) {
151+
throw new HttpErrorException(401, 'test');
152+
}), new DefaultErrorHandler());
153+
154+
$response = $this->httpClient->request(new ClientRequest($this->getAuthority() . "/foo"));
155+
156+
self::assertSame(401, $response->getStatus());
157+
self::assertSame('test', $response->getReason());
158+
}
146159
}

0 commit comments

Comments
 (0)