Skip to content

Commit

Permalink
Merge pull request #107 from pekhota/issue_62_fix_1
Browse files Browse the repository at this point in the history
Added detailed error handling for socket operations. #62
  • Loading branch information
sergeyklay authored Sep 14, 2021
2 parents 0a6a187 + 3e66fb9 commit 98b3af3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Jaeger/ThriftUdpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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');
}
}

Expand Down Expand Up @@ -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));
}
}
15 changes: 15 additions & 0 deletions tests/Jaeger/ThriftUdpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ public function testClose()
$this->expectExceptionMessage('transport is closed');
$this->transport->write('hello');
}

public function testException() {
$this->transport->open();

$this->expectException(TTransportException::class);

$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));
}
}

0 comments on commit 98b3af3

Please sign in to comment.