Skip to content

Commit

Permalink
Introduced helpful messages to exception classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
telkins committed Jan 28, 2020
1 parent 3cf270c commit 7f1c115
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/Exceptions/CircularReferenceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,18 @@

use Exception;

class CircularReferenceException extends Exception {}
class CircularReferenceException extends Exception
{
/**
* Create a new exception instance.
*
* @param string|null $message
* @param mixed|null $code
* @param \Exception|null $previous
* @return void
*/
public function __construct($message = null, $code = null, Exception $previous = null)
{
parent::__construct($message ?? 'This operation caused a circular reference.', $code, $previous);
}
}
14 changes: 13 additions & 1 deletion src/Exceptions/TooManyHopsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@

use Exception;

class TooManyHopsException extends Exception {}
class TooManyHopsException extends Exception
{
/**
* Create a new exception instance.
*
* @param int $maximumAllowableHops
* @return static
*/
public static function make(int $maximumAllowableHops)
{
return new static("This operation exceeded the maximum allowable hops ({$maximumAllowableHops}).");
}
}
2 changes: 1 addition & 1 deletion src/Tasks/AddDagEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected function getNewlyInsertedEdges(DagEdge $edge) : Collection
protected function guardAgainstExceedingMaximumHops(Collection $newEdges)
{
if ($newEdges->isNotEmpty() && ($newEdges->last()->hops > $this->maxHops)) {
throw new TooManyHopsException("Maximum hops ({$this->maxHops}) exceeded");
throw TooManyHopsException::make($this->maxHops);
}
}
}
4 changes: 4 additions & 0 deletions tests/DagManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function it_does_not_allow_circular_references()
* - the expected exception will be thrown
*/
$this->expectException(CircularReferenceException::class);
$this->expectExceptionMessage('This operation caused a circular reference.');

/**
* Act/When:
Expand Down Expand Up @@ -495,6 +496,9 @@ public function it_cannot_exceed_max_hops($initialEdges, $lastStrawEdge)
*/
$this->expectException(TooManyHopsException::class);

/** @todo This assertion to be replace by expectExceptionMessageMatches() from 9.0. */
$this->expectExceptionMessageRegExp('/This operation exceeded the maximum allowable hops \(\d\)\./');

/**
* Act/When:
* - insert the "last straw" edge
Expand Down

0 comments on commit 7f1c115

Please sign in to comment.