-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadosException.php
53 lines (48 loc) · 1.25 KB
/
RadosException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Aternos\Rados\Exception;
use Aternos\Rados\Constants\Constants;
use Aternos\Rados\Generated\Errno;
use Exception;
class RadosException extends Exception
{
/**
* @param int $code
* @return static
*/
public static function fromErrorCode(int $code): static
{
$name = Errno::getErrorName(-$code);
if (function_exists("posix_strerror")) {
$message = posix_strerror(-$code) . " (" . $name . ")";
} else {
$message = "Rados returned error code " . $code . ": " . $name;
}
return new static($message, $code);
}
/**
* @param int $code
* @param bool $throwOnMaxErr
* @return int
* @throws RadosException
*/
public static function handle(int $code, bool $throwOnMaxErr = false): int
{
if ($code < 0 && ($code >= -Constants::MAX_ERRNO || $throwOnMaxErr)) {
throw static::fromErrorCode($code);
}
return $code;
}
/**
* @param Errno ...$errno
* @return bool
*/
public function is(Errno ...$errno): bool
{
foreach ($errno as $e) {
if ($this->getCode() === -$e->value) {
return true;
}
}
return false;
}
}