From 605fab43d7de312190b99ca2f5fcae8dc4aa46b0 Mon Sep 17 00:00:00 2001 From: Alexander Pekhota Date: Tue, 14 Sep 2021 12:55:40 +0300 Subject: [PATCH 1/3] Added detailed error handling for socket operations. #62 --- src/Jaeger/ThriftUdpTransport.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Jaeger/ThriftUdpTransport.php b/src/Jaeger/ThriftUdpTransport.php index deba86e..d52fe72 100644 --- a/src/Jaeger/ThriftUdpTransport.php +++ b/src/Jaeger/ThriftUdpTransport.php @@ -37,6 +37,9 @@ public function __construct(string $host, int $port, LoggerInterface $logger = n $this->host = $host; $this->port = $port; $this->socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + if ($this->socket === false) { + $this->handleError("socket_create failed"); + } $this->logger = $logger ?? new NullLogger(); } @@ -59,7 +62,7 @@ public function open() { $ok = @socket_connect($this->socket, $this->host, $this->port); if ($ok === false) { - throw new TTransportException('socket_connect failed'); + $this->handleError('socket_connect failed'); } } @@ -98,7 +101,15 @@ public function write($buf) $ok = @socket_write($this->socket, $buf); if ($ok === false) { - throw new TTransportException('socket_write failed'); + $this->handleError("socket_write failed"); } } + + public function handleError($msg) + { + $errorCode = socket_last_error($this->socket); + $errorMsg = socket_strerror($errorCode); + + throw new TTransportException(sprintf('%s: [code - %d] %s', $msg, $errorCode, $errorMsg)); + } } From 77d7f7aee7bfc58052b2d2e776f85fb13abc991b Mon Sep 17 00:00:00 2001 From: Alexander Pekhota Date: Tue, 14 Sep 2021 18:43:50 +0300 Subject: [PATCH 2/3] Added tests. #62 --- tests/Jaeger/ThriftUdpTransportTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Jaeger/ThriftUdpTransportTest.php b/tests/Jaeger/ThriftUdpTransportTest.php index 0725361..374a33b 100644 --- a/tests/Jaeger/ThriftUdpTransportTest.php +++ b/tests/Jaeger/ThriftUdpTransportTest.php @@ -37,4 +37,12 @@ public function testClose() $this->expectExceptionMessage('transport is closed'); $this->transport->write('hello'); } + + public function testExceptions() { + $this->transport->open(); + + $this->expectException(TTransportException::class); + $this->expectExceptionMessage("socket_write failed: [code - 40] Message too long"); + $this->transport->write(str_repeat("some string", 10000)); + } } From 3e66fb9801de615493aa3b747affbfbe36d89e6a Mon Sep 17 00:00:00 2001 From: Alexander Pekhota Date: Tue, 14 Sep 2021 19:45:25 +0300 Subject: [PATCH 3/3] fixed test --- tests/Jaeger/ThriftUdpTransportTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/Jaeger/ThriftUdpTransportTest.php b/tests/Jaeger/ThriftUdpTransportTest.php index 374a33b..33a7ad9 100644 --- a/tests/Jaeger/ThriftUdpTransportTest.php +++ b/tests/Jaeger/ThriftUdpTransportTest.php @@ -38,11 +38,18 @@ public function testClose() $this->transport->write('hello'); } - public function testExceptions() { + public function testException() { $this->transport->open(); $this->expectException(TTransportException::class); - $this->expectExceptionMessage("socket_write failed: [code - 40] Message too long"); + + $msgRegEx = "/socket_write failed: \[code - \d+\] Message too long/"; + if (method_exists($this, "expectExceptionMessageRegExp")) { + $this->expectExceptionMessageRegExp($msgRegEx); + } else { + $this->expectExceptionMessageMatches($msgRegEx); + } + $this->transport->write(str_repeat("some string", 10000)); } }