Skip to content

Commit

Permalink
Improved filesystem related exceptions (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Apr 6, 2024
1 parent b3b7961 commit c07def4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function structure_type(array $elements, bool $nullable = false) : StructureType
}

/**
* @param array<string, StructureElement> $elements
* @param array<StructureElement> $elements
*/
function type_structure(array $elements, bool $nullable = false) : StructureType
{
Expand Down
19 changes: 19 additions & 0 deletions src/core/etl/src/Flow/ETL/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Exception;

use Flow\ETL\Filesystem\Path;

final class FileNotFoundException extends Exception
{
public function __construct(public readonly Path $path, ?\Throwable $previous = null)
{
parent::__construct(
\sprintf('No such file or directory: %s ', $this->path->uri()),
0,
$previous
);
}
}
19 changes: 10 additions & 9 deletions src/core/etl/src/Flow/ETL/Filesystem/LocalFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -68,26 +68,26 @@ 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())) {
$this->rm($to);
}

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()) {
Expand All @@ -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));
}
}

Expand All @@ -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()) {
Expand All @@ -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;
Expand Down

0 comments on commit c07def4

Please sign in to comment.