nulib/php/src_os/IOException.php

43 lines
1.2 KiB
PHP
Raw Normal View History

2024-04-25 17:46:18 +04:00
<?php
namespace nulib\os;
use RuntimeException;
/**
* Class IOException: erreur sur un flux ou un système de fichiers
*/
class IOException extends RuntimeException {
static final function last_error(?string $prefix=null): ?self {
$error = error_get_last();
if ($error === null) return null;
$message = $error["message"];
if ($prefix) $message = "$prefix: $message";
return new static($message);
}
static final function generic_error(?string $message=null): self {
if ($message === null) $message = "generic error";
return new static($message);
}
static final function error(?string $prefix=null): self {
$exception = self::last_error($prefix);
if ($exception !== null) throw $exception;
else throw self::generic_error($prefix);
}
static final function ensure_valid($value, bool $throw=true, $invalid=false) {
if ($value !== $invalid) return $value;
elseif (!$throw) return null;
else throw self::error();
}
static final function already_closed(): self {
return new static("already closed");
}
static final function ensure_open(bool $closed): void {
if ($closed) throw self::already_closed();
}
}