| 1 |
<?php |
| 2 |
|
| 3 |
namespace Socket\Raw; |
| 4 |
|
| 5 |
use RuntimeException; |
| 6 |
|
| 7 |
class Exception extends RuntimeException |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Create an Exception after a socket operation on the given $resource failed |
| 11 |
* |
| 12 |
* @param \Socket|resource $resource |
| 13 |
* @param string $messagePrefix |
| 14 |
* @return self |
| 15 |
* @uses socket_last_error() to get last socket error code |
| 16 |
* @uses socket_clear_error() to clear socket error code |
| 17 |
* @uses self::createFromCode() to automatically construct exception with full error message |
| 18 |
*/ |
| 19 |
public static function createFromSocketResource($resource, $messagePrefix = 'Socket operation failed') |
| 20 |
{ |
| 21 |
if (PHP_VERSION_ID >= 80000) { |
| 22 |
try { |
| 23 |
$code = socket_last_error($resource); |
| 24 |
} catch (\Error $e) { |
| 25 |
$code = SOCKET_ENOTSOCK; |
| 26 |
} |
| 27 |
} elseif (is_resource($resource)) { |
| 28 |
$code = socket_last_error($resource); |
| 29 |
socket_clear_error($resource); |
| 30 |
} else { |
| 31 |
// socket already closed, return fixed error code instead of operating on invalid handle |
| 32 |
$code = SOCKET_ENOTSOCK; |
| 33 |
} |
| 34 |
|
| 35 |
return self::createFromCode($code, $messagePrefix); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Create an Exception after a global socket operation failed (like socket creation) |
| 40 |
* |
| 41 |
* @param string $messagePrefix |
| 42 |
* @return self |
| 43 |
* @uses socket_last_error() to get last global error code |
| 44 |
* @uses socket_clear_error() to clear global error code |
| 45 |
* @uses self::createFromCode() to automatically construct exception with full error message |
| 46 |
*/ |
| 47 |
public static function createFromGlobalSocketOperation($messagePrefix = 'Socket operation failed') |
| 48 |
{ |
| 49 |
$code = socket_last_error(); |
| 50 |
socket_clear_error(); |
| 51 |
|
| 52 |
return self::createFromCode($code, $messagePrefix); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Create an Exception for given error $code |
| 57 |
* |
| 58 |
* @param int $code |
| 59 |
* @param string $messagePrefix |
| 60 |
* @return self |
| 61 |
* @throws Exception if given $val is boolean false |
| 62 |
* @uses self::getErrorMessage() to translate error code to error message |
| 63 |
*/ |
| 64 |
public static function createFromCode($code, $messagePrefix = 'Socket error') |
| 65 |
{ |
| 66 |
return new self($messagePrefix . ': ' . self::getErrorMessage($code), $code); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* get error message for given error code |
| 71 |
* |
| 72 |
* @param int $code error code |
| 73 |
* @return string |
| 74 |
* @uses socket_strerror() to translate error code to error message |
| 75 |
* @uses get_defined_constants() to check for related error constant |
| 76 |
*/ |
| 77 |
protected static function getErrorMessage($code) |
| 78 |
{ |
| 79 |
$string = socket_strerror($code); |
| 80 |
|
| 81 |
// search constant starting with SOCKET_ for this error code |
| 82 |
foreach (get_defined_constants() as $key => $value) { |
| 83 |
if($value === $code && strpos($key, 'SOCKET_') === 0) { |
| 84 |
$string .= ' (' . $key . ')'; |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $string; |
| 90 |
} |
| 91 |
} |