<?php namespace nur\b\io; use nur\b\UserException; /** * Class IOException: exception lancée quand une erreur se produit lors d'une * opération sur un flux ou sur un système de fichiers */ class IOException extends UserException { 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_not_false($value, ?string $prefix=null) { if ($value !== false) return $value; else throw self::error($prefix); } 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(); } static final function json_last_error(?string $prefix=null): ?self { $json_last_error = json_last_error(); if ($json_last_error === JSON_ERROR_NONE) return null; $message = json_last_error_msg()." ($json_last_error)"; if ($prefix) $message = "$prefix: $message"; return new static($message); } static final function ensure_json_value($value, ?string $prefix=null) { $exception = self::json_last_error($prefix); if ($exception === null) return $value; else throw $exception; } }