Skip to content

Commit

Permalink
exceptions improved
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jul 16, 2024
1 parent 05ffd4c commit 6e50306
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Dash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Dakujem\Toru;

use BadMethodCallException;
use Dakujem\Toru\Exceptions\BadMethodCallException;
use IteratorAggregate;
use Traversable;

Expand Down
14 changes: 14 additions & 0 deletions src/Exceptions/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Dakujem\Toru\Exceptions;

/**
* A call to a method that does not exist or is not supported.
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
class BadMethodCallException extends \BadMethodCallException implements IndicatesUnintendedToruUsage
{
}
2 changes: 1 addition & 1 deletion src/Exceptions/EmptyCollectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author Andrej Rypák (dakujem) <xrypak@gmail.com>
*/
final class EmptyCollectionException extends RuntimeException
final class EmptyCollectionException extends RuntimeException implements IndicatesExceptionalCase
{
public iterable $input;

Expand Down
14 changes: 14 additions & 0 deletions src/Exceptions/IndicatesExceptionalCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Dakujem\Toru\Exceptions;

/**
* Interface for exceptional cases encountered.
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
interface IndicatesExceptionalCase extends ToruException
{
}
14 changes: 14 additions & 0 deletions src/Exceptions/IndicatesUnintendedToruUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Dakujem\Toru\Exceptions;

/**
* Interface for exceptions indicating invalid use of Toru.
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
interface IndicatesUnintendedToruUsage extends ToruException
{
}
2 changes: 1 addition & 1 deletion src/Exceptions/NoMatchingElementFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author Andrej Rypák (dakujem) <xrypak@gmail.com>
*/
final class NoMatchingElementFound extends RuntimeException
final class NoMatchingElementFound extends RuntimeException implements IndicatesExceptionalCase
{
public iterable $input;
/** @var callable */
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/ToruException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Dakujem\Toru\Exceptions;

use Throwable;

/**
* Base interface for all exceptions thrown by Toru.
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
interface ToruException extends Throwable
{
}
14 changes: 14 additions & 0 deletions src/Exceptions/UnexpectedValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Dakujem\Toru\Exceptions;

/**
* A callable provided a value that is out of range or of invalid type.
*
* @author Andrej Rypak <xrypak@gmail.com>
*/
class UnexpectedValueException extends \UnexpectedValueException implements IndicatesUnintendedToruUsage
{
}
2 changes: 1 addition & 1 deletion src/IteraFn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Dakujem\Toru;

use BadMethodCallException;
use Dakujem\Toru\Exceptions\BadMethodCallException;

/**
* Static factory for partially applied variants of iteration primitives and utilities.
Expand Down
4 changes: 2 additions & 2 deletions src/Regenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Dakujem\Toru;

use Closure;
use Dakujem\Toru\Exceptions\UnexpectedValueException;
use IteratorAggregate;
use LogicException;
use Traversable;

/**
Expand Down Expand Up @@ -34,7 +34,7 @@ public function getIterator(): Traversable
{
$collection = ($this->callable)();
if (!is_iterable($collection)) {
throw new LogicException('The value returned by the provider callable is not an iterable collection.');
throw new UnexpectedValueException('The value returned by the provider callable is not an iterable collection.');
}
return Itera::ensureTraversable(
input: $collection
Expand Down
3 changes: 2 additions & 1 deletion tests/regenerator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Dakujem\Toru\Exceptions\UnexpectedValueException;
use Dakujem\Toru\Itera;
use Dakujem\Toru\Regenerator;
use Tester\Assert;
Expand Down Expand Up @@ -60,5 +61,5 @@ require_once __DIR__ . '/../vendor/autoload.php';
$faulty = new Regenerator(fn() => 'foo');
Assert::throws(function () use ($faulty) {
iterator_to_array($faulty);
}, LogicException::class, 'The value returned by the provider callable is not an iterable collection.');
}, UnexpectedValueException::class, 'The value returned by the provider callable is not an iterable collection.');
})();

0 comments on commit 6e50306

Please sign in to comment.