Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/Hydra/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace ApiPlatform\Core\Hydra\Serializer;

use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\Problem\Serializer\ErrorNormalizerTrait;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand All @@ -26,6 +26,8 @@ final class ErrorNormalizer implements NormalizerInterface
{
const FORMAT = 'jsonld';

use ErrorNormalizerTrait;

private $urlGenerator;
private $debug;

Expand All @@ -40,16 +42,12 @@ public function __construct(UrlGeneratorInterface $urlGenerator, bool $debug = f
*/
public function normalize($object, $format = null, array $context = [])
{
$message = $object->getMessage();
if ($this->debug) {
$trace = $object->getTrace();
} elseif ($object instanceof FlattenException) {
$statusCode = $context['statusCode'] ?? $object->getStatusCode();
if ($statusCode >= 500 && $statusCode < 600) {
$message = Response::$statusTexts[$statusCode];
}
}

$message = $this->getErrorMessage($object, $context, $this->debug);

$data = [
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
'@type' => 'hydra:Error',
Expand Down
5 changes: 4 additions & 1 deletion src/Problem/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class ErrorNormalizer implements NormalizerInterface
{
const FORMAT = 'jsonproblem';

use ErrorNormalizerTrait;

private $debug;

public function __construct(bool $debug = false)
Expand All @@ -37,11 +39,12 @@ public function __construct(bool $debug = false)
*/
public function normalize($object, $format = null, array $context = [])
{
$message = $object->getMessage();
if ($this->debug) {
$trace = $object->getTrace();
}

$message = $this->getErrorMessage($object, $context, $this->debug);

$data = [
'type' => $context['type'] ?? 'https://tools.ietf.org/html/rfc2616#section-10',
'title' => $context['title'] ?? 'An error occurred',
Expand Down
34 changes: 34 additions & 0 deletions src/Problem/Serializer/ErrorNormalizerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Problem\Serializer;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Response;

trait ErrorNormalizerTrait
{
private function getErrorMessage($object, array $context, bool $debug = false): string
{
$message = $object->getMessage();

if ($debug) {
return $message;
} elseif ($object instanceof FlattenException) {
$statusCode = $context['statusCode'] ?? $object->getStatusCode();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot the $context var in the definition of your method 😉

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we have no tests for context \o/ thanks @meyerbaptiste

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in last commit and I added a test

if ($statusCode >= 500 && $statusCode < 600) {
$message = Response::$statusTexts[$statusCode];
}
}

return $message;
}
}
52 changes: 52 additions & 0 deletions tests/Problem/Serializer/ErrorNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ApiPlatform\Core\Problem\Serializer\ErrorNormalizer;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand Down Expand Up @@ -53,4 +54,55 @@ public function testNormalize()
$normalizer->normalize(new \Exception('Hello'), null, ['type' => 'https://dunglas.fr', 'title' => 'Hi'])
);
}

/**
* @dataProvider providerStatusCode
*
* @param $status http status code of the Exception
* @param $originalMessage original message of the Exception
* @param $debug simulates kernel debug variable
*/
public function testErrorServerNormalize($status, $originalMessage, $debug)
{
$normalizer = new ErrorNormalizer($debug);
$exception = FlattenException::create(new \Exception($originalMessage), $status);

$expected = [
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
'title' => 'An error occurred',
'detail' => ($debug || $status < 500) ? $originalMessage : Response::$statusTexts[$status],
];

if ($debug) {
$expected['trace'] = $exception->getTrace();
}

$this->assertEquals($expected, $normalizer->normalize($exception, null, ['statusCode' => $status]));
}

public function providerStatusCode()
{
return [
[Response::HTTP_INTERNAL_SERVER_ERROR, 'Sensitive SQL error displayed', false],
[Response::HTTP_GATEWAY_TIMEOUT, 'Sensitive server error displayed', false],
[Response::HTTP_BAD_REQUEST, 'Bad Request Message', false],
[Response::HTTP_INTERNAL_SERVER_ERROR, 'Sensitive SQL error displayed', true],
[Response::HTTP_GATEWAY_TIMEOUT, 'Sensitive server error displayed', true],
[Response::HTTP_BAD_REQUEST, 'Bad Request Message', true],
];
}

public function testErrorServerNormalizeContextStatus()
{
$normalizer = new ErrorNormalizer(false);
$exception = FlattenException::create(new \Exception(''), 500);

$expected = [
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
'title' => 'An error occurred',
'detail' => Response::$statusTexts[502],
];

$this->assertEquals($expected, $normalizer->normalize($exception, null, ['statusCode' => 502]));
}
}