1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00

Throw FilesytemException on closed file

We may wish to change this later, but at least it is consistent between drivers now.
This commit is contained in:
Aaron Piotrowski 2017-06-16 18:21:56 -05:00
parent 477683f58f
commit 4f9630298f
3 changed files with 14 additions and 10 deletions

View File

@ -31,7 +31,7 @@ class BlockingHandle implements Handle {
*/
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
if ($this->fh === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
$data = \fread($this->fh, $length);
@ -49,7 +49,7 @@ class BlockingHandle implements Handle {
*/
public function write(string $data): Promise {
if ($this->fh === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
$len = \fwrite($this->fh, $data);
@ -76,13 +76,13 @@ class BlockingHandle implements Handle {
*/
public function close(): Promise {
if ($this->fh === null) {
throw new \Error("The file has already been closed");
throw new FilesystemException("The file has already been closed");
}
$fh = $this->fh;
$this->fh = null;
if (\fclose($fh)) {
if (@\fclose($fh)) {
return new Success;
} else {
return new Failure(new FilesystemException(
@ -96,7 +96,7 @@ class BlockingHandle implements Handle {
*/
public function seek(int $position, int $whence = \SEEK_SET): Promise {
if ($this->fh === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
switch ($whence) {
@ -119,7 +119,7 @@ class BlockingHandle implements Handle {
*/
public function tell(): int {
if ($this->fh === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
return \ftell($this->fh);
@ -130,7 +130,7 @@ class BlockingHandle implements Handle {
*/
public function eof(): bool {
if ($this->fh === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
return \feof($this->fh);

View File

@ -80,7 +80,7 @@ class ParallelHandle implements Handle {
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
if ($this->id === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
return new Coroutine($this->doRead($length));
@ -105,7 +105,7 @@ class ParallelHandle implements Handle {
*/
public function write(string $data): Promise {
if ($this->id === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
return new Coroutine($this->doWrite($data));
@ -143,7 +143,7 @@ class ParallelHandle implements Handle {
*/
public function seek(int $offset, int $whence = SEEK_SET): Promise {
if ($this->id === null) {
throw new \Error("The file has been closed");
throw new FilesystemException("The file has been closed");
}
return new Coroutine($this->doSeek($offset, $whence));

View File

@ -144,10 +144,14 @@ abstract class HandleTest extends TestCase {
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testClose() {
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
yield $handle->close();
yield $handle->read();
});
}
}