ApiPlatform\Core\Hydra\Serializer\ErrorNormalizer::normalize PHP Method

normalize() public method

public normalize ( $object, $format = null, array $context = [] )
$context array
    public function normalize($object, $format = null, array $context = [])
    {
        $message = $object->getMessage();
        if ($this->debug) {
            $trace = $object->getTrace();
        }
        $data = ['@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']), '@type' => 'Error', 'hydra:title' => $context['title'] ?? 'An error occurred', 'hydra:description' => $message ?? (string) $object];
        if (isset($trace)) {
            $data['trace'] = $trace;
        }
        return $data;
    }

Usage Example

 public function testNormalize()
 {
     $urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
     $urlGeneratorProphecy->generate('api_jsonld_context', ['shortName' => 'Error'])->willReturn('/context/foo')->shouldBeCalled();
     $normalizer = new ErrorNormalizer($urlGeneratorProphecy->reveal());
     $this->assertEquals(['@context' => '/context/foo', '@type' => 'Error', 'hydra:title' => 'An error occurred', 'hydra:description' => 'Hello'], $normalizer->normalize(new \Exception('Hello')));
     $this->assertEquals(['@context' => '/context/foo', '@type' => 'Error', 'hydra:title' => 'Hi', 'hydra:description' => 'Hello'], $normalizer->normalize(new \Exception('Hello'), null, ['title' => 'Hi']));
 }