Skip to content

Commit 2a0108a

Browse files
committed
Refactor I/O exceptions
1 parent 4ee8339 commit 2a0108a

15 files changed

Lines changed: 42 additions & 42 deletions

src/main/php/web/io/Incomplete.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\io;
22

3-
use io\OperationNotSupportedException;
3+
use io\NotSupported;
44
use io\streams\InputStream;
55
use web\io\Part;
66

@@ -42,9 +42,9 @@ public function kind() { return Part::INCOMPLETE; }
4242
/** @return string */
4343
public function error() { return $this->error; }
4444

45-
/** @return io.IOException */
45+
/** @return io.OperationFailed */
4646
private function notSupported() {
47-
return new OperationNotSupportedException('Cannot read from incomplete part (error= '.$this->error.')');
47+
return new NotSupported('Cannot read from incomplete part (error= '.$this->error.')');
4848
}
4949

5050
/** @return string */
@@ -56,7 +56,7 @@ public function bytes() { throw $this->notSupported(); }
5656
* @param io.Path|io.Folder|io.streams.OutputStream|string $target
5757
* @return iterable
5858
* @throws lang.IllegalArgumentException if filename is invalid
59-
* @throws io.IOException
59+
* @throws io.OperationFailed
6060
*/
6161
public function transmit($target) { throw $this->notSupported(); yield; }
6262

src/main/php/web/io/ReadChunks.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\io;
22

3-
use io\IOException;
3+
use io\OperationFailed;
44
use io\streams\InputStream;
55

66
/**
@@ -18,13 +18,13 @@ class ReadChunks implements InputStream {
1818
* Scans a chunk, populating length and buffer
1919
*
2020
* @return int
21-
* @throws io.IOException for chunked format errors
21+
* @throws io.OperationFailed for chunked format errors
2222
*/
2323
private function scan() {
2424
$size= $this->input->readLine() ?? '';
2525

2626
if (1 !== sscanf($size, '%x', $l)) {
27-
throw new IOException('No chunk segment present (`'.addcslashes($size, "\0..\37").'`)');
27+
throw new OperationFailed('No chunk segment present (`'.addcslashes($size, "\0..\37").'`)');
2828
}
2929

3030
// Chunk with 0 length indicates EOF
@@ -81,7 +81,7 @@ public function read($limit= 8192) {
8181
$chunk= substr($this->buffer, 0, min($limit, $remaining));
8282
if ('' === $chunk) {
8383
$this->remaining= 0;
84-
throw new IOException('Unexpected EOF');
84+
throw new OperationFailed('Unexpected EOF');
8585
}
8686

8787
$length= strlen($chunk);

src/main/php/web/io/ReadLength.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\io;
22

3-
use io\IOException;
3+
use io\OperationFailed;
44
use io\streams\InputStream;
55

66
/**
@@ -34,7 +34,7 @@ public function read($limit= 8192) {
3434
$chunk= $this->input->read(min($limit, $this->remaininig));
3535
if ('' === $chunk) {
3636
$this->remaininig= 0;
37-
throw new IOException('EOF');
37+
throw new OperationFailed('EOF');
3838
}
3939

4040
$this->remaininig-= strlen($chunk);

src/main/php/web/io/Stream.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function bytes() {
7575
* @param io.Path|io.Folder|io.streams.OutputStream|string $target
7676
* @return iterable
7777
* @throws lang.IllegalArgumentException if filename is invalid
78-
* @throws io.IOException
78+
* @throws io.OperationFailed
7979
*/
8080
public function transmit($target) {
8181
if ($target instanceof OutputStream) {

src/main/php/xp/web/Upload.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function bytes() {
6565
* @param io.Path|io.Folder|io.streams.OutputStream|string $target
6666
* @return iterable
6767
* @throws lang.IllegalArgumentException if filename is invalid
68-
* @throws io.IOException
68+
* @throws io.OperationFailed
6969
*/
7070
public function transmit($target) {
7171

src/main/php/xp/web/srv/CannotWrite.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace xp\web\srv;
22

3-
use io\IOException;
3+
use io\OperationFailed;
44

5-
class CannotWrite extends IOException {
5+
class CannotWrite extends OperationFailed {
66

77
/** @return string */
88
public function toString(): string { return $this->message; }

src/main/php/xp/web/srv/Workers.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace xp\web\srv;
22

3-
use io\{Path, IOException};
3+
use io\{Path, OperationFailed};
44
use lang\archive\ArchiveClassLoader;
55
use lang\{Runtime, RuntimeOptions, CommandLine, FileSystemClassLoader};
66
use peer\Socket;
@@ -43,7 +43,7 @@ public function __construct($docroot, $classLoaders) {
4343
/**
4444
* Launches a worker and returns it.
4545
*
46-
* @throws io.IOException
46+
* @throws io.OperationFailed
4747
* @return xp.web.srv.Worker
4848
*/
4949
public function launch() {
@@ -65,7 +65,7 @@ public function launch() {
6565
if ('WINDOWS' !== $os->name()) $commandLine= 'exec '.$commandLine;
6666

6767
if (!($proc= proc_open($commandLine, [STDIN, STDOUT, ['pipe', 'w']], $pipes, null, null, ['bypass_shell' => true]))) {
68-
throw new IOException('Cannot execute `'.$commandLine.'`');
68+
throw new OperationFailed('Cannot execute `'.$commandLine.'`');
6969
}
7070

7171
// Parse `[...] PHP 8.3.15 Development Server (http://127.0.0.1:60922) started`
@@ -78,7 +78,7 @@ public function launch() {
7878
if (!preg_match('/\([a-z]+:\/\/([0-9.]+):([0-9]+)\)/', $line, $matches)) {
7979
proc_terminate($proc, 2);
8080
proc_close($proc);
81-
throw new IOException('Cannot determine bound port: `'.implode('', $lines).'`');
81+
throw new OperationFailed('Cannot determine bound port: `'.implode('', $lines).'`');
8282
}
8383

8484
return new Worker($proc, new Socket($matches[1], (int)$matches[2]));

src/test/php/web/unittest/io/IncompleteTest.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\unittest\io;
22

3-
use io\OperationNotSupportedException;
3+
use io\NotSupported;
44
use test\{Assert, Expect, Test};
55
use web\io\{Incomplete, Part};
66

@@ -29,20 +29,20 @@ public function string_representation() {
2929
);
3030
}
3131

32-
#[Test, Expect(OperationNotSupportedException::class)]
32+
#[Test, Expect(NotSupported::class)]
3333
public function cannot_access_bytes() {
3434
(new Incomplete('upload', UPLOAD_ERR_INI_SIZE))->bytes();
3535
}
3636

37-
#[Test, Expect(OperationNotSupportedException::class)]
37+
#[Test, Expect(NotSupported::class)]
3838
public function cannot_transmit() {
3939
$it= (new Incomplete('upload', UPLOAD_ERR_INI_SIZE))->transmit('./uploads');
4040
while ($it->valid()) {
4141
$it->next();
4242
}
4343
}
4444

45-
#[Test, Expect(OperationNotSupportedException::class)]
45+
#[Test, Expect(NotSupported::class)]
4646
public function cannot_read_bytes() {
4747
(new Incomplete('upload', UPLOAD_ERR_INI_SIZE))->read();
4848
}

src/test/php/web/unittest/io/PartsTest.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\unittest\io;
22

3-
use io\OperationNotSupportedException;
3+
use io\NotSupported;
44
use io\streams\{InputStream, MemoryInputStream, Streams};
55
use lang\FormatException;
66
use test\{Assert, Expect, Test, Values};
@@ -213,7 +213,7 @@ public function can_skip_reading_parts() {
213213
Assert::equals('...', $bytes);
214214
}
215215

216-
#[Test, Expect(OperationNotSupportedException::class)]
216+
#[Test, Expect(NotSupported::class)]
217217
public function cannot_read_from_incomplete_file() {
218218
$parts= $this->parts(
219219
'--%1$s',

src/test/php/web/unittest/io/ReadChunksTest.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace web\unittest\io;
22

3-
use io\IOException;
3+
use io\OperationFailed;
44
use test\{Assert, Expect, Test, Values};
55
use web\io\{ReadChunks, TestInput};
66
use web\unittest\Chunking;
@@ -23,13 +23,13 @@ public function can_create() {
2323
new ReadChunks($this->input("0\r\n\r\n"));
2424
}
2525

26-
#[Test, Expect(IOException::class)]
26+
#[Test, Expect(OperationFailed::class)]
2727
public function raises_exception_from_read_when_non_chunked_data_appears() {
2828
$fixture= new ReadChunks($this->input(''));
2929
$fixture->read();
3030
}
3131

32-
#[Test, Expect(IOException::class)]
32+
#[Test, Expect(OperationFailed::class)]
3333
public function raises_exception_from_available_when_non_chunked_data_appears() {
3434
$fixture= new ReadChunks($this->input(''));
3535
$fixture->available();
@@ -130,7 +130,7 @@ public function raises_exception_on_eof_in_the_middle_of_data() {
130130
$fixture= new ReadChunks($this->input("ff\r\n...💣"));
131131
$fixture->read();
132132

133-
Assert::throws(IOException::class, fn() => $fixture->read(1));
133+
Assert::throws(OperationFailed::class, fn() => $fixture->read(1));
134134
}
135135

136136
#[Test, Values([4, 8192])]

0 commit comments

Comments
 (0)