diff --git a/src/Exception.php b/src/Exception.php index bdabf78..d20cde1 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -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; } } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index a6a4060..4f4bc2f 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -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()); + } }