diff --git a/CHANGELOG.md b/CHANGELOG.md index aaeab35..d559da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +1.5.0 +===== + +* (feature) Add normalization stack for better error reporting. + + 1.4.1 ===== diff --git a/src/Normalizer/SimpleNormalizer.php b/src/Normalizer/SimpleNormalizer.php index 6bd02da..bfc8ed2 100644 --- a/src/Normalizer/SimpleNormalizer.php +++ b/src/Normalizer/SimpleNormalizer.php @@ -25,6 +25,7 @@ class SimpleNormalizer { private readonly ?ClassMetadataFactory $doctrineMetadata; + private const string STACK_CONTEXT = "simple-normalizer.debug-stack"; /** * @param ServiceLocator $objectNormalizers @@ -95,6 +96,13 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed return $value; } + if (!isset($context[self::STACK_CONTEXT]) || !\is_array($context[self::STACK_CONTEXT])) + { + $context[self::STACK_CONTEXT] = []; + } + + $context[self::STACK_CONTEXT][] = get_debug_type($value); + if (\is_array($value)) { return $this->recursiveNormalizeArray($value, $context); @@ -121,15 +129,17 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed catch (ServiceNotFoundException $exception) { throw new ObjectTypeNotSupportedException(\sprintf( - "Can't normalize type %s", + "Can't normalize type '%s' in stack %s", get_debug_type($value), + implode(" > ", array_reverse($context[self::STACK_CONTEXT])), ), 0, $exception); } } throw new UnsupportedTypeException(\sprintf( - "Can't normalize type %s", + "Can't normalize type %s in stack %s", get_debug_type($value), + implode(" > ", array_reverse($context[self::STACK_CONTEXT])), )); } diff --git a/tests/Normalizer/ObjectNormalizationTest.php b/tests/Normalizer/ObjectNormalizationTest.php index c15e848..d4ab1f8 100644 --- a/tests/Normalizer/ObjectNormalizationTest.php +++ b/tests/Normalizer/ObjectNormalizationTest.php @@ -67,7 +67,7 @@ public function testNonEmptyStdClassIsInvalid () : void $object->prop = 5; $this->expectException(ObjectTypeNotSupportedException::class); - $this->expectExceptionMessage("Can't normalize type stdClass"); + $this->expectExceptionMessage("Can't normalize type 'stdClass' in stack stdClass"); $normalizer->normalize($object); } diff --git a/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php b/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php index 2187885..123cab3 100644 --- a/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php +++ b/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php @@ -36,6 +36,10 @@ public function testContextPassing () : void [ "test" => 123, "o" => "hai", + "simple-normalizer.debug-stack" => [ + get_debug_type($value), + DummyVO::class, + ], ], );