Skip to content

Commit

Permalink
Improve exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Mar 18, 2019
1 parent 217e66a commit 9ede8fa
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
6 changes: 1 addition & 5 deletions source/PropTypes/Concerns/ArrayOfConcern.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ private static function isValidArrayOf($value, $definition): bool
{
if (!is_array($value)) {
$actual = is_object($value) ? get_class($value) : gettype($value);
throw new InvalidArgumentException("{$definition->name} expects {$definition->type}[] but {$actual} provided");
}

if (!is_object($definition->definition) || !is_a($definition->definition, Definition::class)) {
throw new InvalidArgumentException("arrayOf missing type definition");
throw new InvalidArgumentException("'{$definition->name}' expected {$definition->type} but got {$actual}");
}

foreach ($value as $next) {
Expand Down
2 changes: 1 addition & 1 deletion source/PropTypes/Concerns/ObjectOfConcern.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private static function isValidObjectOf($value, $definition): bool

if (!$isObject || !is_a($value, $type)) {
$actual = $isObject ? get_class($value) : gettype($value);
throw new InvalidArgumentException("{$actual} is not objectOf({$type})");
throw new InvalidArgumentException("'{$definition->name}' expected {$type} but got {$actual}");
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion source/PropTypes/Concerns/TypesConcern.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function __callStatic(string $method, $parameters = null)
return $definition;
}

throw new InvalidArgumentException("{$method} is not a valid type");
throw new InvalidArgumentException("'{$method}' is not a valid type");
}
}
12 changes: 8 additions & 4 deletions source/PropTypes/Concerns/ValidationConcern.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ trait ValidationConcern
public static function validate(array $definitions, array $properties)
{
foreach ($definitions as $key => $definition) {
if (!is_object($definition)) {
throw new InvalidArgumentException("'{$key}' is not a valid type");
}

$definition->name = $key;
static::validatePresence($definition, $key, $properties);

if (isset($properties[$key]) && $definition->either) {
foreach ($definition->either as $next) {
try {
static::validateValue($next, $key, $properties[$key]);
// one of them passed...§
// one of them passed...
continue 2;
} catch (Exception $e) {
// try the next type...
continue;
}

throw new InvalidArgumentException("{$key} was neither of the types defined");
throw new InvalidArgumentException("'{$key}' was neither of the types defined");
}
}

Expand All @@ -42,7 +46,7 @@ private static function validatePresence(Definition $definition, string $key, ar
}

if (!isset($properties[$key]) && $definition->isRequired) {
throw new InvalidArgumentException("{$key} is required but missing");
throw new InvalidArgumentException("'{$key}' was required but not provided");
}
}

Expand All @@ -55,7 +59,7 @@ private static function validateValue(Definition $definition, $key, $value)

if (!static::{$function}($value, $definition)) {
$actual = gettype($value);
throw new InvalidArgumentException("{$key} expects {$expected} but {$actual} provided");
throw new InvalidArgumentException("'{$key}' expected {$expected} but got {$actual}");
}
}
}
6 changes: 3 additions & 3 deletions tests/Concerns/ArrayOfConcernTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class ArrayOfConcernTest extends TestCase
{
public function test_it_can_tell_when_isnt_array()
{
$this->expectException(InvalidArgumentException::class, "arrayOf expects string[] but integer provided");
$this->expectException(InvalidArgumentException::class, "'arrayOf' expected string but got integer");

$definitions = [
"arrayOf" => PropTypes::arrayOf(PropTypes::string())->isRequired(),
Expand All @@ -23,7 +23,7 @@ public function test_it_can_tell_when_isnt_array()

public function test_it_can_tell_when_has_a_bad_type_definition()
{
$this->expectException(InvalidArgumentException::class, "arrayOf expects string[] but integer provided");
$this->expectException(InvalidArgumentException::class, "'foo' is not a valid type");

$definitions = [
"arrayOf" => PropTypes::arrayOf("foo")->isRequired(),
Expand All @@ -38,7 +38,7 @@ public function test_it_can_tell_when_has_a_bad_type_definition()

public function test_it_can_tell_items_types()
{
$this->expectException(InvalidArgumentException::class, "arrayOf has an unexpected integer in arrayOf(string)");
$this->expectException(InvalidArgumentException::class, "'arrayOf' expected string but got integer");

$definitions = [
"arrayOf" => PropTypes::arrayOf(PropTypes::int())->isRequired(),
Expand Down
4 changes: 2 additions & 2 deletions tests/Concerns/EitherConcernTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test_it_ignores_is_required_on_sub_types()

public function test_obeys_is_required()
{
$this->expectException(InvalidArgumentException::class, "value is required but missing");
$this->expectException(InvalidArgumentException::class, "'value' is required but missing");

$definitions = [
"value" => PropTypes::either(PropTypes::string(), PropTypes::int())
Expand All @@ -59,7 +59,7 @@ public function test_obeys_is_required()

public function test_it_rejects_either_type()
{
$this->expectException(InvalidArgumentException::class, "value was neither of the types defined");
$this->expectException(InvalidArgumentException::class, "'value' was neither of the types defined");

$definitions = [
"value" => PropTypes::either(PropTypes::string(), PropTypes::int())
Expand Down
4 changes: 2 additions & 2 deletions tests/Concerns/ObjectOfConcernTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class ObjectOfConcernTest extends TestCase
{
public function test_it_can_tell_non_objects()
{
$this->expectException(InvalidArgumentException::class, "integer is not objectOf(stdClass)");
$this->expectException(InvalidArgumentException::class, "'objectOf' expected stdClass but got integer");

$definitions = [
"objectOf" => PropTypes::objectOf(stdClass::class)->isRequired(),
Expand All @@ -23,7 +23,7 @@ public function test_it_can_tell_non_objects()

public function test_it_can_tell_objects_with_wrong_type()
{
$this->expectException(InvalidArgumentException::class, "Definition is not objectOf(stdClass)");
$this->expectException(InvalidArgumentException::class, "'objectOf' expected stdClass but got Definition");

$definitions = [
"objectOf" => PropTypes::objectOf(stdClass::class)->isRequired(),
Expand Down
6 changes: 3 additions & 3 deletions tests/PropTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_it_can_allow_null_for_optional_values()

public function test_it_can_require_values()
{
$this->expectException(InvalidArgumentException::class, "name is required but missing");
$this->expectException(InvalidArgumentException::class, "'name' is required but missing");

$definitions = [
"name" => PropTypes::string()->isRequired(),
Expand Down Expand Up @@ -62,7 +62,7 @@ public function test_it_can_validate_correct_values()

public function test_it_can_validate_incorrect_values()
{
$this->expectException(InvalidArgumentException::class, "name expects string but integer provided");
$this->expectException(InvalidArgumentException::class, "'name' expected string but got integer");

$definitions = [
"name" => PropTypes::string()->isRequired(),
Expand Down Expand Up @@ -116,7 +116,7 @@ public function test_it_can_validate_a_range_of_types()

public function test_it_can_recognise_unrecognised_types()
{
$this->expectException(InvalidArgumentException::class, "unknown is not a valid type");
$this->expectException(InvalidArgumentException::class, "'unknown' is not a valid type");

PropTypes::unknown();
}
Expand Down

0 comments on commit 9ede8fa

Please sign in to comment.