From 66d580c8abd987ade18a8803ffff01f6b888b114 Mon Sep 17 00:00:00 2001 From: Nikola Svetozarevic Date: Fri, 13 Dec 2024 17:33:01 +0100 Subject: [PATCH] Apply feedback changes --- src/Exceptions/CannotCreateData.php | 3 ++- src/Resolvers/DataFromArrayResolver.php | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Exceptions/CannotCreateData.php b/src/Exceptions/CannotCreateData.php index b867f224d..11a2d2c05 100644 --- a/src/Exceptions/CannotCreateData.php +++ b/src/Exceptions/CannotCreateData.php @@ -24,6 +24,7 @@ public static function noNormalizerFound(string $dataClass, mixed $value): self public static function constructorMissingParameters( DataClass $dataClass, array $parameters, + Throwable $previous, ): self { $parameters = collect($parameters); @@ -41,6 +42,6 @@ public static function constructorMissingParameters( ->map(fn (DataProperty|DataParameter $parameter) => $parameter->name) ->join(', ')}."; - return new self($message); + return new self($message, previous: $previous); } } diff --git a/src/Resolvers/DataFromArrayResolver.php b/src/Resolvers/DataFromArrayResolver.php index 0a241ff45..bf7edc55d 100644 --- a/src/Resolvers/DataFromArrayResolver.php +++ b/src/Resolvers/DataFromArrayResolver.php @@ -93,14 +93,19 @@ protected function createData( } } - if ($this->isAnyParameterMissing($dataClass, array_keys($parameters))) { - throw CannotCreateData::constructorMissingParameters( - $dataClass, - $parameters, - ); + try { + return new $dataClass->name(...$parameters); + } catch (ArgumentCountError $error) { + if ($this->isAnyParameterMissing($dataClass, array_keys($parameters))) { + throw CannotCreateData::constructorMissingParameters( + $dataClass, + $parameters, + $error + ); + } else { + throw $error; + } } - - return new $dataClass->name(...$parameters); } protected function isAnyParameterMissing(DataClass $dataClass, array $parameters): bool