Skip to content

Commit

Permalink
[feature] add callback as string to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jan 11, 2021
1 parent 8f6d7ab commit b759cfc
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ public static function createFor($value): self
* Invoke the callable with the passed arguments. Arguments of type
* Zenstruck\Callback\Parameter are resolved before invoking.
*
* @param mixed ...$arguments
* @param mixed|Parameter ...$arguments
*
* @return mixed
*
* @throws UnresolveableArgument
* @throws \ArgumentCountError If there is a argument count mismatch
* @throws UnresolveableArgument If the argument cannot be resolved
*/
public function invoke(...$arguments)
{
Expand All @@ -75,7 +76,7 @@ public function invoke(...$arguments)
try {
$arguments[$key] = $argument->resolve($parameters[$key]);
} catch (UnresolveableArgument $e) {
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s".', $key + 1, $argument->type()), $e);
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s". (%s)', $key + 1, $argument->type(), $this), $e);
}
}

Expand All @@ -90,21 +91,22 @@ public function invoke(...$arguments)
*
* @return mixed
*
* @throws UnresolveableArgument
* @throws \ArgumentCountError If the number of arguments is less than $min
* @throws UnresolveableArgument If the argument cannot be resolved
*/
public function invokeAll(Parameter $parameter, int $min = 0)
{
$arguments = $this->function->getParameters();

if (\count($arguments) < $min) {
throw new \ArgumentCountError("{$min} argument(s) of type \"{$parameter->type()}\" required.");
throw new \ArgumentCountError("{$min} argument(s) of type \"{$parameter->type()}\" required ({$this}).");
}

foreach ($arguments as $key => $argument) {
try {
$arguments[$key] = $parameter->resolve($argument);
} catch (UnresolveableArgument $e) {
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s"', $key + 1, $parameter->type()), $e);
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s". (%s)', $key + 1, $parameter->type(), $this), $e);
}
}

Expand Down

0 comments on commit b759cfc

Please sign in to comment.