Skip to content

Commit 2a05501

Browse files
committed
Add normalized types stack for better error reporting
1 parent d9fd95a commit 2a05501

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
1.5.0
2+
=====
3+
4+
* (feature) Add normalization stack for better error reporting.
5+
6+
17
1.4.1
28
=====
39

src/Normalizer/SimpleNormalizer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class SimpleNormalizer
2626
{
2727
private readonly ?ClassMetadataFactory $doctrineMetadata;
28+
private const string STACK_CONTEXT = "simple-normalizer.debug-stack";
2829

2930
/**
3031
* @param ServiceLocator<SimpleObjectNormalizerInterface> $objectNormalizers
@@ -95,6 +96,13 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed
9596
return $value;
9697
}
9798

99+
if (!isset($context[self::STACK_CONTEXT]) || !\is_array($context[self::STACK_CONTEXT]))
100+
{
101+
$context[self::STACK_CONTEXT] = [];
102+
}
103+
104+
$context[self::STACK_CONTEXT][] = get_debug_type($value);
105+
98106
if (\is_array($value))
99107
{
100108
return $this->recursiveNormalizeArray($value, $context);
@@ -121,15 +129,17 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed
121129
catch (ServiceNotFoundException $exception)
122130
{
123131
throw new ObjectTypeNotSupportedException(\sprintf(
124-
"Can't normalize type %s",
132+
"Can't normalize type '%s' in stack %s",
125133
get_debug_type($value),
134+
implode(" > ", array_reverse($context[self::STACK_CONTEXT])),
126135
), 0, $exception);
127136
}
128137
}
129138

130139
throw new UnsupportedTypeException(\sprintf(
131-
"Can't normalize type %s",
140+
"Can't normalize type %s in stack %s",
132141
get_debug_type($value),
142+
implode(" > ", array_reverse($context[self::STACK_CONTEXT])),
133143
));
134144
}
135145

tests/Normalizer/ObjectNormalizationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testNonEmptyStdClassIsInvalid () : void
6767
$object->prop = 5;
6868

6969
$this->expectException(ObjectTypeNotSupportedException::class);
70-
$this->expectExceptionMessage("Can't normalize type stdClass");
70+
$this->expectExceptionMessage("Can't normalize type 'stdClass' in stack stdClass");
7171
$normalizer->normalize($object);
7272
}
7373

tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function testContextPassing () : void
3636
[
3737
"test" => 123,
3838
"o" => "hai",
39+
"simple-normalizer.debug-stack" => [
40+
get_debug_type($value),
41+
DummyVO::class,
42+
],
3943
],
4044
);
4145

0 commit comments

Comments
 (0)