ApiPlatform\Core\Problem\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 = ['type' => $context['type'] ?? 'https://tools.ietf.org/html/rfc2616#section-10', 'title' => $context['title'] ?? 'An error occurred', 'detail' => $message ?? (string) $object];
        if (isset($trace)) {
            $data['trace'] = $trace;
        }
        return $data;
    }

Usage Example

 public function testNormalize()
 {
     $normalizer = new ErrorNormalizer();
     $this->assertEquals(['type' => 'https://tools.ietf.org/html/rfc2616#section-10', 'title' => 'An error occurred', 'detail' => 'Hello'], $normalizer->normalize(new \Exception('Hello')));
     $this->assertEquals(['type' => 'https://dunglas.fr', 'title' => 'Hi', 'detail' => 'Hello'], $normalizer->normalize(new \Exception('Hello'), null, ['type' => 'https://dunglas.fr', 'title' => 'Hi']));
 }