|
12 | 12 | namespace Symfony\Component\Serializer\Normalizer;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
| 15 | +use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
15 | 16 |
|
16 | 17 | /**
|
17 | 18 | * Normalizes errors according to the API Problem spec (RFC 7807).
|
@@ -40,20 +41,24 @@ public function __construct(bool $debug = false, array $defaultContext = [])
|
40 | 41 | *
|
41 | 42 | * @return array
|
42 | 43 | */
|
43 |
| - public function normalize($exception, $format = null, array $context = []) |
| 44 | + public function normalize($object, $format = null, array $context = []) |
44 | 45 | {
|
| 46 | + if (!$object instanceof FlattenException) { |
| 47 | + throw new InvalidArgumentException(sprintf('The object must implement "%s".', FlattenException::class)); |
| 48 | + } |
| 49 | + |
45 | 50 | $context += $this->defaultContext;
|
46 | 51 | $debug = $this->debug && ($context['debug'] ?? true);
|
47 | 52 |
|
48 | 53 | $data = [
|
49 | 54 | 'type' => $context['type'],
|
50 | 55 | 'title' => $context['title'],
|
51 |
| - 'status' => $context['status'] ?? $exception->getStatusCode(), |
52 |
| - 'detail' => $debug ? $exception->getMessage() : $exception->getStatusText(), |
| 56 | + 'status' => $context['status'] ?? $object->getStatusCode(), |
| 57 | + 'detail' => $debug ? $object->getMessage() : $object->getStatusText(), |
53 | 58 | ];
|
54 | 59 | if ($debug) {
|
55 |
| - $data['class'] = $exception->getClass(); |
56 |
| - $data['trace'] = $exception->getTrace(); |
| 60 | + $data['class'] = $object->getClass(); |
| 61 | + $data['trace'] = $object->getTrace(); |
57 | 62 | }
|
58 | 63 |
|
59 | 64 | return $data;
|
|
0 commit comments