diff --git a/src/Buffer.php b/src/Buffer.php index e5aa2f6..af33c93 100644 --- a/src/Buffer.php +++ b/src/Buffer.php @@ -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); } } } diff --git a/tests/SocketTest.php b/tests/SocketTest.php index 479b3ec..e44331b 100644 --- a/tests/SocketTest.php +++ b/tests/SocketTest.php @@ -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); @@ -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()