Skip to content

Commit

Permalink
Improve error reporting to include errno and error constant name
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Apr 3, 2022
1 parent 9ae6a96 commit d03fb7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ protected static function getErrorMessage($code)
{
$string = socket_strerror($code);

// search constant starting with SOCKET_ for this error code
// search constant starting with SOCKET_E* for this error code
foreach (get_defined_constants() as $key => $value) {
if($value === $code && strpos($key, 'SOCKET_') === 0) {
$string .= ' (' . $key . ')';
if($value === $code && strpos($key, 'SOCKET_E') === 0) {
$string .= ' (' . substr($key, 7) . ')';
break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ public function testCreateFromSocketResourceWithClosedResourceThrows()
$this->assertInstanceOf('Socket\Raw\Exception', $exception);
$this->assertEquals(SOCKET_ENOTSOCK, $exception->getCode());
}

public function testCreateFromCodeCreatesExceptionWithMessageAndErrorConstantNameAndValue()
{
$exception = Exception::createFromCode(SOCKET_ECONNREFUSED);

$this->assertInstanceOf('Socket\Raw\Exception', $exception);
$this->assertEquals('Socket error: ' . socket_strerror(SOCKET_ECONNREFUSED) . ' (ECONNREFUSED)', $exception->getMessage());
$this->assertEquals(SOCKET_ECONNREFUSED, $exception->getCode());
}
}

0 comments on commit d03fb7f

Please sign in to comment.