diff --git a/src/Psl/Type/Exception/AssertException.php b/src/Psl/Type/Exception/AssertException.php index a5a31c8f..9e4adb1a 100644 --- a/src/Psl/Type/Exception/AssertException.php +++ b/src/Psl/Type/Exception/AssertException.php @@ -17,25 +17,8 @@ final class AssertException extends Exception /** * @param list $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', @@ -58,8 +41,17 @@ public static function withValue( ?Throwable $previous = null ): self { $paths = $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, Vec\filter_nulls($paths), $previous); } public static function withoutValue( @@ -69,6 +61,12 @@ public static function withoutValue( ): self { $paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path]; - return new self(null, $expected_type, Vec\filter_nulls($paths), $previous); + $message = Str\format( + 'Expected "%s", received no value at path "%s".', + $expected_type, + Str\join($paths, '.'), + ); + + return new self(null, $expected_type, $message, Vec\filter_nulls($paths), $previous); } } diff --git a/src/Psl/Type/Exception/CoercionException.php b/src/Psl/Type/Exception/CoercionException.php index 6c5f2275..dddf72c8 100644 --- a/src/Psl/Type/Exception/CoercionException.php +++ b/src/Psl/Type/Exception/CoercionException.php @@ -17,27 +17,8 @@ final class CoercionException extends Exception /** * @param list $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', @@ -60,8 +41,18 @@ public static function withValue( ?Throwable $previous = null ): self { $paths = $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, Vec\filter_nulls($paths), $previous); } public static function withoutValue( @@ -71,6 +62,13 @@ public static function withoutValue( ): self { $paths = $previous instanceof Exception ? [$path, ...$previous->getPaths()] : [$path]; - return new self(null, $target, Vec\filter_nulls($paths), $previous); + $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, $message, Vec\filter_nulls($paths), $previous); } }