From c07def4759fcd8a6fbd9157c83be7ce9299427d8 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz <1921950+norberttech@users.noreply.github.com> Date: Sat, 6 Apr 2024 18:23:57 +0200 Subject: [PATCH] Improved filesystem related exceptions (#1041) --- .../Tests/Integration/CSVExtractorTest.php | 5 ++--- .../ETL/Adapter/Filesystem/FlysystemFS.php | 10 +++++----- src/core/etl/src/Flow/ETL/DSL/functions.php | 2 +- .../ETL/Exception/FileNotFoundException.php | 19 +++++++++++++++++++ .../Flow/ETL/Filesystem/LocalFilesystem.php | 19 ++++++++++--------- 5 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 src/core/etl/src/Flow/ETL/Exception/FileNotFoundException.php diff --git a/src/adapter/etl-adapter-csv/tests/Flow/ETL/Adapter/CSV/Tests/Integration/CSVExtractorTest.php b/src/adapter/etl-adapter-csv/tests/Flow/ETL/Adapter/CSV/Tests/Integration/CSVExtractorTest.php index 0f617c394..33c908f78 100644 --- a/src/adapter/etl-adapter-csv/tests/Flow/ETL/Adapter/CSV/Tests/Integration/CSVExtractorTest.php +++ b/src/adapter/etl-adapter-csv/tests/Flow/ETL/Adapter/CSV/Tests/Integration/CSVExtractorTest.php @@ -7,10 +7,9 @@ use function Flow\ETL\Adapter\CSV\{from_csv, to_csv}; use function Flow\ETL\DSL\{df, from_array, print_schema, ref}; use Flow\ETL\Adapter\CSV\CSVExtractor; -use Flow\ETL\Exception\RuntimeException; use Flow\ETL\Extractor\Signal; use Flow\ETL\Filesystem\{LocalFilesystem, Path}; -use Flow\ETL\{Config, ConfigBuilder, Flow, FlowContext, Row, Rows}; +use Flow\ETL\{Config, ConfigBuilder, Exception\FileNotFoundException, Flow, FlowContext, Row, Rows}; use PHPUnit\Framework\TestCase; final class CSVExtractorTest extends TestCase @@ -334,7 +333,7 @@ public function test_limit() : void public function test_load_not_existing_file_throws_exception() : void { - $this->expectException(RuntimeException::class); + $this->expectException(FileNotFoundException::class); $extractor = from_csv(Path::realpath('not_existing_file.csv')); $generator = $extractor->extract(new FlowContext(Config::default())); \iterator_to_array($generator); diff --git a/src/adapter/etl-adapter-filesystem/src/Flow/ETL/Adapter/Filesystem/FlysystemFS.php b/src/adapter/etl-adapter-filesystem/src/Flow/ETL/Adapter/Filesystem/FlysystemFS.php index dc4da599d..6e17ddfe4 100644 --- a/src/adapter/etl-adapter-filesystem/src/Flow/ETL/Adapter/Filesystem/FlysystemFS.php +++ b/src/adapter/etl-adapter-filesystem/src/Flow/ETL/Adapter/Filesystem/FlysystemFS.php @@ -4,7 +4,7 @@ namespace Flow\ETL\Adapter\Filesystem; -use Flow\ETL\Exception\{InvalidArgumentException, MissingDependencyException, RuntimeException}; +use Flow\ETL\Exception\{FileNotFoundException, InvalidArgumentException, MissingDependencyException}; use Flow\ETL\Filesystem; use Flow\ETL\Filesystem\Path; use Flow\ETL\Filesystem\Stream\{FileStream, Mode}; @@ -69,11 +69,11 @@ public function fileExists(Path $path) : bool public function mv(Path $from, Path $to) : void { if ($from->isPattern() || $to->isPattern()) { - throw new RuntimeException("Pattern paths can't be moved: " . $from->uri() . ' -> ' . $to->uri()); + throw new InvalidArgumentException("Pattern paths can't be moved: " . $from->uri() . ' -> ' . $to->uri()); } if ($from->scheme() !== $to->scheme()) { - throw new RuntimeException("Can't move path from different schemes: " . $from->scheme() . ' -> ' . $to->scheme()); + throw new InvalidArgumentException("Can't move path from different schemes: " . $from->scheme() . ' -> ' . $to->scheme()); } if ($this->fileExists($to)) { @@ -130,7 +130,7 @@ public function rm(Path $path) : void return; } - throw new RuntimeException("Can't remove path because it does not exists, path: " . $path->uri()); + throw new FileNotFoundException($path); } /** @@ -143,7 +143,7 @@ public function rm(Path $path) : void public function scan(Path $path, PartitionFilter $partitionFilter = new NoopFilter()) : \Generator { if (!$path->isPattern() && !$this->fileExists($path)) { - throw new RuntimeException(\sprintf('Path "%s" does not exists', $path->uri())); + throw new FileNotFoundException($path); } $fs = $this->factory->create($path); diff --git a/src/core/etl/src/Flow/ETL/DSL/functions.php b/src/core/etl/src/Flow/ETL/DSL/functions.php index 996f3b417..06d66f928 100644 --- a/src/core/etl/src/Flow/ETL/DSL/functions.php +++ b/src/core/etl/src/Flow/ETL/DSL/functions.php @@ -301,7 +301,7 @@ function structure_type(array $elements, bool $nullable = false) : StructureType } /** - * @param array $elements + * @param array $elements */ function type_structure(array $elements, bool $nullable = false) : StructureType { diff --git a/src/core/etl/src/Flow/ETL/Exception/FileNotFoundException.php b/src/core/etl/src/Flow/ETL/Exception/FileNotFoundException.php new file mode 100644 index 000000000..399068c89 --- /dev/null +++ b/src/core/etl/src/Flow/ETL/Exception/FileNotFoundException.php @@ -0,0 +1,19 @@ +path->uri()), + 0, + $previous + ); + } +} diff --git a/src/core/etl/src/Flow/ETL/Filesystem/LocalFilesystem.php b/src/core/etl/src/Flow/ETL/Filesystem/LocalFilesystem.php index 92a0d4e50..586660d4e 100644 --- a/src/core/etl/src/Flow/ETL/Filesystem/LocalFilesystem.php +++ b/src/core/etl/src/Flow/ETL/Filesystem/LocalFilesystem.php @@ -4,7 +4,7 @@ namespace Flow\ETL\Filesystem; -use Flow\ETL\Exception\{InvalidArgumentException, RuntimeException}; +use Flow\ETL\Exception\{FileNotFoundException, InvalidArgumentException, RuntimeException}; use Flow\ETL\Filesystem; use Flow\ETL\Filesystem\Stream\{FileStream, Mode}; use Flow\ETL\Partition\{NoopFilter, PartitionFilter}; @@ -68,11 +68,11 @@ public function fileExists(Path $path) : bool public function mv(Path $from, Path $to) : void { if (!$from->isLocal() || !$to->isLocal()) { - throw new RuntimeException(\sprintf('Paths "%s" and "%s" are not local', $from->uri(), $to->uri())); + throw new InvalidArgumentException(\sprintf('Paths "%s" and "%s" are not local', $from->uri(), $to->uri())); } if ($from->isPattern() || $to->isPattern()) { - throw new RuntimeException('Pattern paths can\'t be moved'); + throw new InvalidArgumentException('Pattern paths can\'t be moved'); } if (\file_exists($to->path())) { @@ -80,14 +80,14 @@ public function mv(Path $from, Path $to) : void } if (!\rename($from->path(), $to->path())) { - throw new RuntimeException(\sprintf('Can\'t move "%s" to "%s"', $from->uri(), $to->uri())); + throw new InvalidArgumentException(\sprintf('Can\'t move "%s" to "%s"', $from->uri(), $to->uri())); } } public function open(Path $path, Mode $mode) : FileStream { if (!$path->isLocal()) { - throw new RuntimeException(\sprintf('Path "%s" is not local', $path->uri())); + throw new InvalidArgumentException(\sprintf('Path "%s" is not local', $path->uri())); } if ($path->isPattern()) { @@ -96,7 +96,7 @@ public function open(Path $path, Mode $mode) : FileStream if (!$this->directoryExists($path->parentDirectory())) { if (!\mkdir($concurrentDirectory = $path->parentDirectory()->path(), recursive: true) && !\is_dir($concurrentDirectory)) { - throw new \RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory)); + throw new RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory)); } } @@ -106,7 +106,7 @@ public function open(Path $path, Mode $mode) : FileStream public function rm(Path $path) : void { if (!$path->isLocal()) { - throw new RuntimeException(\sprintf('Path "%s" is not local', $path->uri())); + throw new InvalidArgumentException(\sprintf('Path "%s" is not local', $path->uri())); } if (!$path->isPattern()) { @@ -133,13 +133,14 @@ public function rm(Path $path) : void public function scan(Path $path, PartitionFilter $partitionFilter = new NoopFilter()) : \Generator { if (!$path->isLocal()) { - throw new RuntimeException(\sprintf('Path "%s" is not local', $path->uri())); + throw new InvalidArgumentException(\sprintf('Path "%s" is not local', $path->uri())); } if (!$path->isPattern()) { if (!$this->fileExists($path)) { - throw new RuntimeException(\sprintf('Path "%s" does not exists', $path->uri())); + throw new FileNotFoundException($path); } + yield $path; return;