Skip to content

Commit

Permalink
Moved exception message generation to named constructors
Browse files Browse the repository at this point in the history
in AssertException and CoercionException
  • Loading branch information
zerkms committed Oct 28, 2024
1 parent 93af1e7 commit 6b02ca3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
42 changes: 20 additions & 22 deletions src/Psl/Type/Exception/AssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,8 @@ final class AssertException extends Exception
/**
* @param list<string> $paths
*/
private function __construct(?string $actual, string $expected, array $paths = [], ?Throwable $previous = null)
private function __construct(?string $actual, string $expected, string $message, array $paths = [], ?Throwable $previous = null)
{
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual;

if ($first !== null) {
$message = Str\format(
'Expected "%s", got "%s"%s.',
$expected,
$first,
$paths ? ' at path "' . Str\join($paths, '.') . '"' : '',
);
} else {
$message = Str\format(
'Expected "%s", received no value at path "%s".',
$expected,
Str\join($paths, '.'),
);
}

parent::__construct(
$message,
$actual ?? 'null',
Expand All @@ -57,18 +40,33 @@ public static function withValue(
?string $path = null,
?Throwable $previous = null
): self {
$paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path];
$paths = Vec\filter_nulls($previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path]);
$actual = get_debug_type($value);
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual;

return new self(get_debug_type($value), $expected_type, Vec\filter_nulls($paths), $previous);
$message = Str\format(
'Expected "%s", got "%s"%s.',
$expected_type,
$first,
$paths ? ' at path "' . Str\join($paths, '.') . '"' : '',
);

return new self($actual, $expected_type, $message, $paths, $previous);
}

public static function withoutValue(
string $expected_type,
?string $path = null,
?Throwable $previous = null
): self {
$paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path];
$paths = Vec\filter_nulls($previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path]);

$message = Str\format(
'Expected "%s", received no value at path "%s".',
$expected_type,
Str\join($paths, '.'),
);

return new self(null, $expected_type, Vec\filter_nulls($paths), $previous);
return new self(null, $expected_type, $message, $paths, $previous);
}
}
46 changes: 22 additions & 24 deletions src/Psl/Type/Exception/CoercionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,8 @@ final class CoercionException extends Exception
/**
* @param list<string> $paths
*/
private function __construct(?string $actual, string $target, array $paths = [], ?Throwable $previous = null)
private function __construct(?string $actual, string $target, string $message, array $paths = [], ?Throwable $previous = null)
{
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual;

if ($first !== null) {
$message = Str\format(
'Could not coerce "%s" to type "%s"%s%s.',
$first,
$target,
$paths ? ' at path "' . Str\join($paths, '.') . '"' : '',
$previous && !$previous instanceof self ? ': ' . $previous->getMessage() : '',
);
} else {
$message = Str\format(
'Could not coerce to type "%s" at path "%s" as the value was not passed%s.',
$target,
Str\join($paths, '.'),
$previous && !$previous instanceof self ? ': ' . $previous->getMessage() : '',
);
}

parent::__construct(
$message,
$actual ?? 'null',
Expand All @@ -59,18 +40,35 @@ public static function withValue(
?string $path = null,
?Throwable $previous = null
): self {
$paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path];
$paths = Vec\filter_nulls($previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path]);
$actual = get_debug_type($value);
$first = $previous instanceof Exception ? $previous->getFirstFailingActualType() : $actual;

return new self(get_debug_type($value), $target, Vec\filter_nulls($paths), $previous);
$message = Str\format(
'Could not coerce "%s" to type "%s"%s%s.',
$first,
$target,
$paths ? ' at path "' . Str\join($paths, '.') . '"' : '',
$previous && !$previous instanceof self ? ': ' . $previous->getMessage() : '',
);

return new self($actual, $target, $message, $paths, $previous);
}

public static function withoutValue(
string $target,
?string $path = null,
?Throwable $previous = null
): self {
$paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path];
$paths = Vec\filter_nulls($previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path]);

$message = Str\format(
'Could not coerce to type "%s" at path "%s" as the value was not passed%s.',
$target,
Str\join($paths, '.'),
$previous && !$previous instanceof self ? ': ' . $previous->getMessage() : '',
);

return new self(null, $target, Vec\filter_nulls($paths), $previous);
return new self(null, $target, $message, $paths, $previous);
}
}

0 comments on commit 6b02ca3

Please sign in to comment.