Skip to content

Commit

Permalink
Revert to throwing WebsocketClosedException
Browse files Browse the repository at this point in the history
Extending StreamException instead to conform to stream interface.
  • Loading branch information
trowski committed Dec 28, 2023
1 parent f3aa702 commit 3f20dab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Internal/Rfc6455FrameHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Amp\ByteStream\ReadableIterableStream;
use Amp\ByteStream\ReadableStream;
use Amp\ByteStream\StreamException;
use Amp\DeferredFuture;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
Expand All @@ -20,6 +19,7 @@
use Amp\Websocket\Parser\WebsocketParser;
use Amp\Websocket\Parser\WebsocketParserException;
use Amp\Websocket\WebsocketCloseCode;
use Amp\Websocket\WebsocketClosedException;
use Amp\Websocket\WebsocketCloseInfo;
use Amp\Websocket\WebsocketHeartbeatQueue;
use Amp\Websocket\WebsocketMessage;
Expand Down Expand Up @@ -271,7 +271,11 @@ public function close(int $code, string $reason = '', bool $byPeer = false): voi

$this->messageQueue->complete();

$this->currentMessageQueue?->error(new StreamException('Connection closed while streaming message body'));
$this->currentMessageQueue?->error(new WebsocketClosedException(
'Connection closed while streaming message body',
$code,
$reason,
));
$this->currentMessageQueue = null;

if ($this->socket->isClosed()) {
Expand Down
4 changes: 3 additions & 1 deletion src/WebsocketException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Amp\Websocket;

class WebsocketException extends \Exception
use Amp\ByteStream\StreamException;

class WebsocketException extends StreamException
{
}
6 changes: 4 additions & 2 deletions test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Amp\Websocket\Test;

use Amp\ByteStream\StreamException;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Socket\Socket;
use Amp\Websocket\Parser\Rfc6455ParserFactory;
use Amp\Websocket\Parser\WebsocketFrameType;
use Amp\Websocket\Rfc6455Client;
use Amp\Websocket\WebsocketCloseCode;
use Amp\Websocket\WebsocketClosedException;
use function Amp\delay;

class ParserTest extends AsyncTestCase
Expand Down Expand Up @@ -46,8 +46,10 @@ public function testParser(
$this->assertSame($isBinary, $message->isBinary());
$client->close();
}
} catch (StreamException $exception) {
} catch (WebsocketClosedException $exception) {
$this->assertStringContainsString('Connection closed', $exception->getMessage());
$this->assertSame($code, $exception->getCode());
$this->assertSame($reason, $exception->getReason());
}

$closeInfo = $client->getCloseInfo();
Expand Down

0 comments on commit 3f20dab

Please sign in to comment.