Skip to content

Commit

Permalink
PosixErorrs: named error constants
Browse files Browse the repository at this point in the history
  • Loading branch information
vasa-c committed Oct 15, 2023
1 parent 25f3aef commit 1613f2f
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1 (15.10.2023)

* Error constants (`PosixErrors`)

## 0.1.0 (14.10.2023)

* The interface (`IPosix`)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Are located in `axy\posix\exceptions`:

* `IPosixException` - the main interface
* `PosixException` - a standard function error (extends `LogicException`, just because)
* The constructor takes an error code as only argument
* An exception object has readonly properties:
* `$posixErrorCode (int)` - the origin error code, is equal to the exception code
* `$posixErrorConstant (?string)` - the error constant name (`EPERM` for example, NULL if not defined)
* `$posixErrorMessage (string)` - the error message as `posix_strerror()` returned
* `PosixNotImplementedException` - the corresponding function is not defined (for example, `getpgid` is not defined on all systems)

## Structures
Expand All @@ -51,6 +56,11 @@ All constants that used as method arguments are collected in the `PosixConstants
These are just copies of [the standard constants](https://www.php.net/manual/en/posix.constants.access.php) (without the prefix `POSIX_`).
The PHP version allows collect these in the `IPosix` but this would clutter the interface with rarely used elements.

## Error codes

Error codes constants are collected in `PosixErrors`.
There is a method `PosixErrors::getConstName(int $code): ?string` that returns the constant name for a code.

## Listener

Listeners for `RealPosix` allow to make a logger or a simple mock.
Expand Down
148 changes: 148 additions & 0 deletions src/PosixErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace axy\posix;

use ReflectionClass;

final class PosixErrors
{
public const EPERM = 1;
public const ENOENT = 2;
public const ESRCH = 3;
public const EINTR = 4;
public const EIO = 5;
public const ENXIO = 6;
public const E2BIG = 7;
public const ENOEXEC = 8;
public const EBADF = 9;
public const ECHILD = 10;
public const EAGAIN = 11;
public const ENOMEM = 12;
public const EACCES = 13;
public const EFAULT = 14;
public const ENOTBLK = 15;
public const EBUSY = 16;
public const EEXIST = 17;
public const EXDEV = 18;
public const ENODEV = 19;
public const ENOTDIR = 20;
public const EISDIR = 21;
public const EINVAL = 22;
public const ENFILE = 23;
public const EMFILE = 24;
public const ENOTTY = 25;
public const ETXTBSY = 26;
public const EFBIG = 27;
public const ENOSPC = 28;
public const ESPIPE = 29;
public const EROFS = 30;
public const EMLINK = 31;
public const EPIPE = 32;
public const EDOM = 33;
public const ERANGE = 34;
public const EDEADLK = 35;
public const ENAMETOOLONG = 36;
public const ENOLCK = 37;
public const ENOSYS = 38;
public const ENOTEMPTY = 39;
public const ELOOP = 40;
public const ENOMSG = 42;
public const EIDRM = 43;
public const ECHRNG = 44;
public const EL2NSYNC = 45;
public const EL3HLT = 46;
public const EL3RST = 47;
public const ELNRNG = 48;
public const EUNATCH = 49;
public const ENOCSI = 50;
public const EL2HLT = 51;
public const EBADE = 52;
public const EBADR = 53;
public const EXFULL = 54;
public const ENOANO = 55;
public const EBADRQC = 56;
public const EBADSLT = 57;
public const EBFONT = 59;
public const ENOSTR = 60;
public const ENODATA = 61;
public const ETIME = 62;
public const ENOSR = 63;
public const ENONET = 64;
public const ENOPKG = 65;
public const EREMOTE = 66;
public const ENOLINK = 67;
public const EADV = 68;
public const ESRMNT = 69;
public const ECOMM = 70;
public const EPROTO = 71;
public const EMULTIHOP = 72;
public const EDOTDOT = 73;
public const EBADMSG = 74;
public const EOVERFLOW = 75;
public const ENOTUNIQ = 76;
public const EBADFD = 77;
public const EREMCHG = 78;
public const ELIBACC = 79;
public const ELIBBAD = 80;
public const ELIBSCN = 81;
public const ELIBMAX = 82;
public const ELIBEXEC = 83;
public const EILSEQ = 84;
public const ERESTART = 85;
public const ESTRPIPE = 86;
public const EUSERS = 87;
public const ENOTSOCK = 88;
public const EDESTADDRREQ = 89;
public const EMSGSIZE = 90;
public const EPROTOTYPE = 91;
public const ENOPROTOOPT = 92;
public const EPROTONOSUPPORT = 93;
public const ESOCKTNOSUPPORT = 94;
public const EOPNOTSUPP = 95;
public const EPFNOSUPPORT = 96;
public const EAFNOSUPPORT = 97;
public const EADDRINUSE = 98;
public const EADDRNOTAVAIL = 99;
public const ENETDOWN = 100;
public const ENETUNREACH = 101;
public const ENETRESET = 102;
public const ECONNABORTED = 103;
public const ECONNRESET = 104;
public const ENOBUFS = 105;
public const EISCONN = 106;
public const ENOTCONN = 107;
public const ESHUTDOWN = 108;
public const ETOOMANYREFS = 109;
public const ETIMEDOUT = 110;
public const ECONNREFUSED = 111;
public const EHOSTDOWN = 112;
public const EHOSTUNREACH = 113;
public const EALREADY = 114;
public const EINPROGRESS = 115;
public const ESTALE = 116;
public const EUCLEAN = 117;
public const ENOTNAM = 118;
public const ENAVAIL = 119;
public const EISNAM = 120;
public const EREMOTEIO = 121;
public const EDQUOT = 122;
public const ENOMEDIUM = 123;
public const EMEDIUMTYPE = 124;
public const ECANCELED = 125;
public const ENOKEY = 126;
public const EKEYEXPIRED = 127;
public const EKEYREVOKED = 128;
public const EKEYREJECTED = 129;
public const EOWNERDEAD = 130;
public const ENOTRECOVERABLE = 131;
public const ERFKILL = 132;
public const EHWPOISON = 133;

public static function getConstName(int $code): ?string
{
$class = new ReflectionClass(self::class);
return array_search($code, $class->getConstants(), true) ?: null;
}
}
14 changes: 12 additions & 2 deletions src/exceptions/PosixException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@

namespace axy\posix\exceptions;

use axy\posix\PosixErrors;
use LogicException;

class PosixException extends LogicException
{
public function __construct(int $code)
public readonly ?string $posixErrorConstant;
public readonly string $posixErrorMessage;

public function __construct(public readonly int $posixErrorCode)
{
parent::__construct((string)posix_strerror($code), $code);
$this->posixErrorConstant = PosixErrors::getConstName($posixErrorCode);
$this->posixErrorMessage = (string)posix_strerror($posixErrorCode);
$message = $this->posixErrorMessage;
if ($this->posixErrorConstant !== null) {
$message = "{$this->posixErrorConstant}: $message";
}
parent::__construct($message, $posixErrorCode);
}
}
18 changes: 18 additions & 0 deletions tests/PosixErrorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace axy\posix\tests;

use axy\posix\PosixErrors;

class PosixErrorsTest extends BaseTestCase
{
public function testGetConstName(): void
{
$this->assertSame('EDESTADDRREQ', PosixErrors::getConstName(PosixErrors::EDESTADDRREQ));
$this->assertSame('EADV', PosixErrors::getConstName(PosixErrors::EADV));
$this->assertNull(PosixErrors::getConstName(-5));
$this->assertNull(PosixErrors::getConstName(500));
}
}
6 changes: 5 additions & 1 deletion tests/exceptions/PosixExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class PosixExceptionTest extends BaseTestCase
public function testMessage(): void
{
$e = new PosixException(5);
$str = posix_strerror(5);
$this->assertSame(5, $e->getCode());
$this->assertSame(posix_strerror(5), $e->getMessage());
$this->assertSame(5, $e->posixErrorCode);
$this->assertSame('EIO', $e->posixErrorConstant);
$this->assertSame($str, $e->posixErrorMessage);
$this->assertSame("EIO: $str", $e->getMessage());
}
}

0 comments on commit 1613f2f

Please sign in to comment.