Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error reporting when custom error handler is used #49

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,25 @@ protected function handleResume()

protected function handleWrite($data, $remoteAddress)
{
$errstr = '';
\set_error_handler(function ($_, $error) use (&$errstr) {
// Match errstr from PHP's warning message.
// stream_socket_sendto(): Message too long\n
$errstr = \trim($error);
});

if ($remoteAddress === null) {
// do not use fwrite() as it obeys the stream buffer size and
// packets are not to be split at 8kb
$ret = @\stream_socket_sendto($this->socket, $data);
$ret = \stream_socket_sendto($this->socket, $data);
} else {
$ret = @\stream_socket_sendto($this->socket, $data, 0, $remoteAddress);
$ret = \stream_socket_sendto($this->socket, $data, 0, $remoteAddress);
}

\restore_error_handler();

if ($ret < 0 || $ret === false) {
$error = \error_get_last();
throw new Exception('Unable to send packet: ' . \trim($error['message']));
throw new Exception('Unable to send packet: ' . $errstr);
}
}
}
10 changes: 9 additions & 1 deletion tests/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testClientSendAfterEndIsNoop(Socket $client)
$this->loop->run();
}

public function testClientSendHugeWillFail()
public function testClientSendHugeWillFailWithoutCallingCustomErrorHandler()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
$client = Block\await($promise, $this->loop);
Expand All @@ -95,7 +95,15 @@ public function testClientSendHugeWillFail()
$client->on('error', $this->expectCallableOnce());
$client->end();

$error = null;
set_error_handler(function ($_, $errstr) use (&$error) {
$error = $errstr;
});

$this->loop->run();

restore_error_handler();
$this->assertNull($error);
}

public function testClientSendNoServerWillFail()
Expand Down