Skip to content

Commit

Permalink
Refactor error handling in RowInterfaceProvider and RowListInterfaceP…
Browse files Browse the repository at this point in the history
…rovider

In this commit, we've improved error handling in two classes - RowInterfaceProvider and RowListInterfaceProvider. If a SQLFileNotFound exception occurs, a warning is logged and the code tries to provide an instance with the given name. This change helps make our exceptions more transparent and our error recovery more robust.
  • Loading branch information
koriym committed Jun 11, 2024
1 parent c45e7c8 commit deef126
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/RowInterfaceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Ray\Di\InjectorInterface;
use Ray\Di\ProviderInterface;
use Ray\Query\Exception\SqlFileNotFoundException;
use Throwable;

use function error_log;
use function sprintf;

/** @implements ProviderInterface<QueryInterface> */
final class RowInterfaceProvider implements ProviderInterface
Expand All @@ -23,6 +25,8 @@ final class RowInterfaceProvider implements ProviderInterface

/** @var SqlFinder */
private $finder;

/** @var InjectorInterface */
private $injector;

public function __construct(
Expand All @@ -41,14 +45,19 @@ public function get(): QueryInterface
{
try {
$sql = ($this->finder)($this->ip->getParameter());
} catch (SqlFileNotFoundException | Throwable $e) {
// For development
// @codeCoverageIgnoreStart
} catch (SqlFileNotFoundException $e) {
try {
$named = $e->sql;
$instance = $this->injector->getInstance(RowInterface::class, $named);
error_log(sprintf('Warning: #[Sql(\'%s\')] is not vald. Change to #[\\Ray\\Di\\Di\\Named(\'%s\')]', $named, $named));

return $this->injector->getInstance(RowInterface::class, $named);
return $instance;
} catch (Unbound $unbound) {
throw $e;
}
// @codeCoverageIgnoreEnd
}

return new SqlQueryRow($this->pdo, $sql);
Expand Down
24 changes: 22 additions & 2 deletions src/RowListInterfaceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Ray\Di\ProviderInterface;
use Ray\Query\Exception\SqlFileNotFoundException;

use function assert;
use function error_log;
use function sprintf;

/** @implements ProviderInterface<QueryInterface> */
final class RowListInterfaceProvider implements ProviderInterface
{
Expand All @@ -22,6 +26,8 @@ final class RowListInterfaceProvider implements ProviderInterface

/** @var SqlFinder */
private $finder;

/** @var InjectorInterface */
private $injector;

public function __construct(
Expand All @@ -43,14 +49,28 @@ public function get(): QueryInterface
} catch (SqlFileNotFoundException $e) {
try {
$named = $e->sql;
// Try "RowListInterface"
$instance = $this->injector->getInstance(RowListInterface::class, $named);
// For development
// @codeCoverageIgnoreStart
assert($instance instanceof QueryInterface);
error_log(sprintf('Warning: #[Sql(\'%s\')] is not vald. Change to #[\\Ray\\Di\\Di\\Named(\'%s\')]', $named, $named));

return $this->injector->getInstance(RowListInterface::class, $named);
return $instance;
} catch (Unbound $unbound) {
try {
return $this->injector->getInstance('', $named);
assert(isset($named)); // @phpstan-ignore-line
// try "callable"
/** @var QueryInterface $instance */
$instance = $this->injector->getInstance('', $named);
assert($instance instanceof QueryInterface);
error_log(sprintf('Warning: #[Sql(\'%s\')] is not vald. Change to #[\\Ray\\Di\\Di\\Named(\'%s\')]', $named, $named));

return $instance;
} catch (Unbound $unbound) {
throw $e;
}
// @codeCoverageIgnoreEnd
}
}

Expand Down

0 comments on commit deef126

Please sign in to comment.