1
0
mirror of https://github.com/danog/file.git synced 2024-11-29 20:09:10 +01:00

Use StreamExceptions in Handle, make FilesystemException not extend StreamException

This commit is contained in:
Niklas Keller 2017-06-21 13:51:59 +02:00
parent a13bb6f721
commit d2c1409bf4
5 changed files with 45 additions and 26 deletions

View File

@ -3,6 +3,7 @@
namespace Amp\File;
use Amp\ByteStream\ClosedException;
use Amp\ByteStream\StreamException;
use Amp\Failure;
use Amp\Promise;
use Amp\Success;
@ -44,7 +45,7 @@ class BlockingHandle implements Handle {
return new Success(\strlen($data) ? $data : null);
}
return new Failure(new FilesystemException(
return new Failure(new StreamException(
"Failed reading from file handle"
));
}
@ -63,7 +64,7 @@ class BlockingHandle implements Handle {
return new Success($len);
}
return new Failure(new FilesystemException(
return new Failure(new StreamException(
"Failed writing to file handle"
));
}
@ -87,7 +88,7 @@ class BlockingHandle implements Handle {
*/
public function close(): Promise {
if ($this->fh === null) {
throw new FilesystemException("The file has already been closed");
throw new ClosedException("The file has already been closed");
}
$fh = $this->fh;
@ -97,7 +98,7 @@ class BlockingHandle implements Handle {
return new Success;
}
return new Failure(new FilesystemException(
return new Failure(new StreamException(
"Failed closing file handle"
));
}
@ -107,7 +108,7 @@ class BlockingHandle implements Handle {
*/
public function seek(int $position, int $whence = \SEEK_SET): Promise {
if ($this->fh === null) {
throw new FilesystemException("The file has been closed");
throw new ClosedException("The file has been closed");
}
switch ($whence) {
@ -115,7 +116,7 @@ class BlockingHandle implements Handle {
case \SEEK_CUR:
case \SEEK_END:
if (@\fseek($this->fh, $position, $whence) === -1) {
return new Failure(new FilesystemException("Could not seek in file"));
return new Failure(new StreamException("Could not seek in file"));
}
return new Success($this->tell());
default:
@ -130,7 +131,7 @@ class BlockingHandle implements Handle {
*/
public function tell(): int {
if ($this->fh === null) {
throw new FilesystemException("The file has been closed");
throw new ClosedException("The file has been closed");
}
return \ftell($this->fh);
@ -141,7 +142,7 @@ class BlockingHandle implements Handle {
*/
public function eof(): bool {
if ($this->fh === null) {
throw new FilesystemException("The file has been closed");
throw new ClosedException("The file has been closed");
}
return \feof($this->fh);

View File

@ -3,6 +3,7 @@
namespace Amp\File;
use Amp\ByteStream\ClosedException;
use Amp\ByteStream\StreamException;
use Amp\Deferred;
use Amp\Promise;
use Amp\Success;
@ -62,9 +63,12 @@ class EioHandle implements Handle {
$this->isActive = false;
if ($result === -1) {
$deferred->fail(new ClosedException(
sprintf('Reading from file failed: %s.', \eio_get_last_error($req))
));
$error = \eio_get_last_error($req);
if ($error === "Bad file descriptor") {
$deferred->fail(new ClosedException("Reading from the file failed due to a closed handle"));
} else {
$deferred->fail(new StreamException("Reading from the file failed:" . $error));
}
} else {
$this->position += \strlen($result);
$deferred->resolve(\strlen($result) ? $result : null);
@ -145,9 +149,12 @@ class EioHandle implements Handle {
}
if ($result === -1) {
$deferred->fail(new ClosedException(
sprintf('Writing to the file failed: %s', \eio_get_last_error($req))
));
$error = \eio_get_last_error($req);
if ($error === "Bad file descriptor") {
$deferred->fail(new ClosedException("Writing to the file failed due to a closed handle"));
} else {
$deferred->fail(new StreamException("Writing to the file failed: " . $error));
}
} else {
$this->position += $result;
if ($this->position > $this->size) {
@ -172,7 +179,8 @@ class EioHandle implements Handle {
private function onClose($deferred, $result, $req) {
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
$error = \eio_get_last_error($req);
$deferred->fail(new StreamException($error));
} else {
$deferred->resolve();
}

View File

@ -2,9 +2,7 @@
namespace Amp\File;
use Amp\ByteStream\StreamException;
class FilesystemException extends StreamException {
class FilesystemException extends \Exception {
public function __construct(string $message, \Throwable $previous = null) {
parent::__construct($message, 0, $previous);
}

View File

@ -3,6 +3,7 @@
namespace Amp\File;
use Amp\ByteStream\ClosedException;
use Amp\ByteStream\StreamException;
use Amp\Coroutine;
use Amp\Parallel\Worker\TaskException;
use Amp\Parallel\Worker\Worker;
@ -110,9 +111,9 @@ class ParallelHandle implements Handle {
$this->position += \strlen($data);
return $data;
} catch (TaskException $exception) {
throw new FilesystemException("Reading from the file failed", $exception);
throw new StreamException("Reading from the file failed", 0, $exception);
} catch (WorkerException $exception) {
throw new FilesystemException("Sending the task to the worker failed", $exception);
throw new StreamException("Sending the task to the worker failed", 0, $exception);
} finally {
$this->busy = false;
}
@ -159,9 +160,9 @@ class ParallelHandle implements Handle {
try {
$length = yield $this->worker->enqueue(new Internal\FileTask('fwrite', [$data], $this->id));
} catch (TaskException $exception) {
throw new FilesystemException("Writing to the file failed", $exception);
throw new StreamException("Writing to the file failed", 0, $exception);
} catch (WorkerException $exception) {
throw new FilesystemException("Sending the task to the worker failed", $exception);
throw new StreamException("Sending the task to the worker failed", 0, $exception);
} finally {
if (--$this->pendingWrites === 0) {
$this->busy = false;
@ -205,9 +206,9 @@ class ParallelHandle implements Handle {
return $this->position;
} catch (TaskException $exception) {
throw new FilesystemException('Seeking in the file failed.', $exception);
throw new StreamException('Seeking in the file failed.', 0, $exception);
} catch (WorkerException $exception) {
throw new FilesystemException("Sending the task to the worker failed", $exception);
throw new StreamException("Sending the task to the worker failed", 0, $exception);
} finally {
$this->busy = false;
}

View File

@ -3,6 +3,7 @@
namespace Amp\File;
use Amp\ByteStream\ClosedException;
use Amp\ByteStream\StreamException;
use Amp\Deferred;
use Amp\File\Internal\UvPoll;
use Amp\Loop;
@ -58,7 +59,12 @@ class UvHandle implements Handle {
$this->isActive = false;
if ($result < 0) {
$deferred->fail(new ClosedException(\uv_strerror($result)));
$error = \uv_strerror($result);
if ($error === "bad file descriptor") {
$deferred->fail(new ClosedException("Reading from the file failed due to a closed handle"));
} else {
$deferred->fail(new StreamException("Reading from the file failed: " . $error));
}
} else {
$length = strlen($buffer);
$this->position = $this->position + $length;
@ -132,7 +138,12 @@ class UvHandle implements Handle {
}
if ($result < 0) {
$deferred->fail(new ClosedException(\uv_strerror($result)));
$error = \uv_strerror($result);
if ($error === "bad file descriptor") {
$deferred->fail(new ClosedException("Writing to the file failed due to a closed handle"));
} else {
$deferred->fail(new StreamException("Writing to the file failed: " . $error));
}
} else {
StatCache::clear($this->path);
$newPosition = $this->position + $length;