Skip to content

Commit 4b580fe

Browse files
committed
ClientException extends SteamException
Extending StreamException prevents read() and buffer() from violating the ReadableStream interface.
1 parent e984728 commit 4b580fe

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

src/ClientException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Amp\Http\Server;
44

5+
use Amp\ByteStream\StreamException;
6+
57
/**
68
* A ClientException thrown from {@see RequestBody::read()} or {@see RequestBody::buffer()} indicates that the
79
* requesting client stream has been closed due to an error or exceeding a server limit such as the body size limit.
@@ -17,7 +19,7 @@
1719
* Responses returned by request handlers after a ClientException has been thrown will be ignored, as a response has
1820
* already been generated by the error handler.
1921
*/
20-
class ClientException extends \Exception
22+
class ClientException extends StreamException
2123
{
2224
/** @internal Do not instantiate instances of this exception in your request handlers or middleware! */
2325
public function __construct(

src/Driver/Http1Driver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,13 +749,13 @@ static function (int $bodySize) use (&$bodySizeLimit): void {
749749
$this->pendingResponse->await(); // Wait for response to be generated.
750750
$this->pendingResponseCount--;
751751
} while ($this->continue);
752-
} catch (StreamException) {
753-
// Client disconnected, finally block will clean up.
754752
} catch (ClientException $exception) {
755753
if ($this->bodyQueue === null || !$this->pendingResponseCount) {
756754
// Send an error response only if another response has not already been sent to the request.
757755
$this->sendErrorResponse($exception, $request ?? null)->await();
758756
}
757+
} catch (StreamException) {
758+
// Client disconnected, finally block will clean up.
759759
} finally {
760760
$this->pendingResponse->finally(function (): void {
761761
$this->removeTimeout();

src/Driver/Http2Driver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function initializeWriting(
165165

166166
if ($this->settings !== null) {
167167
// Upgraded connections automatically assume an initial stream with ID 1.
168-
// No date will be incoming on this stream, so body size of 0.
168+
// No data will be incoming on this stream, so body size of 0.
169169
$this->createStream(1, 0, Http2Stream::RESERVED | Http2Stream::REMOTE_CLOSED);
170170
$this->remoteStreamId = \max(1, $this->remoteStreamId);
171171
$this->remainingStreams--;
@@ -364,11 +364,11 @@ private function send(int $id, Response $response, Request $request, Cancellatio
364364
$id,
365365
);
366366
}
367-
} catch (StreamException|CancelledException) {
368-
// Client disconnected, ignore and proceed to clean up below.
369-
$chunk = null;
370367
} catch (ClientException $exception) {
371368
$error = $exception->getCode() ?? Http2Parser::CANCEL; // Set error code to be used below.
369+
} catch (StreamException|CancelledException) {
370+
// Body stream threw or client disconnected, ignore and proceed to clean up below.
371+
$chunk = null;
372372
} catch (\Throwable $throwable) {
373373
// Will be rethrown after cleanup below.
374374
}

src/RequestBody.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* This class allows streamed and buffered access to a request body with an API similar to {@see Payload}.
1414
*
15-
* The {@see read()} and {@see buffer()} methods can also throw {@see ClientException} in addition to the usual
15+
* The {@see read()} and {@see buffer()} methods can throw {@see ClientException}, which extends
1616
* {@see StreamException}, though generally there is no need to catch this exception.
1717
*
1818
* Additionally, this class allows increasing the body size limit dynamically.
@@ -37,7 +37,6 @@ public function __construct(
3737

3838
/**
3939
* @throws ClientException
40-
* @throws StreamException
4140
*/
4241
public function read(?Cancellation $cancellation = null): ?string
4342
{
@@ -46,7 +45,7 @@ public function read(?Cancellation $cancellation = null): ?string
4645

4746
/**
4847
* @see Payload::buffer()
49-
* @throws ClientException|BufferException|StreamException
48+
* @throws ClientException|BufferException
5049
*/
5150
public function buffer(?Cancellation $cancellation = null, int $limit = \PHP_INT_MAX): string
5251
{
@@ -92,7 +91,7 @@ public function increaseSizeLimit(int $size): void
9291
* Buffers entire stream before returning. Use {@see self::buffer()} to optionally provide a {@see Cancellation}
9392
* and/or length limit.
9493
*
95-
* @throws ClientException|BufferException|StreamException
94+
* @throws ClientException|BufferException
9695
*/
9796
public function __toString(): string
9897
{

0 commit comments

Comments
 (0)