-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #2182 add a SerializerErrorRenderer (xabbuh)
This PR was merged into the 2.x branch. Discussion ---------- add a SerializerErrorRenderer Commits ------- ca200e5 add a SerializerErrorRenderer
- Loading branch information
Showing
8 changed files
with
289 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\ErrorRenderer; | ||
|
||
use FOS\RestBundle\Context\Context; | ||
use FOS\RestBundle\Serializer\Serializer; | ||
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; | ||
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Serializer\Exception\NotEncodableValueException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SerializerErrorRenderer implements ErrorRendererInterface | ||
{ | ||
private $serializer; | ||
private $format; | ||
private $fallbackErrorRenderer; | ||
private $debug; | ||
|
||
/** | ||
* @param string|callable(FlattenException) $format | ||
* @param string|bool $debug | ||
*/ | ||
public function __construct(Serializer $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false) | ||
{ | ||
if (!is_string($format) && !is_callable($format)) { | ||
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a string or a callable, "%s" given.', __METHOD__, \is_object($format) ? \get_class($format) : \gettype($format))); | ||
} | ||
|
||
if (!is_bool($debug) && !is_callable($debug)) { | ||
throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be a boolean or a callable, "%s" given.', __METHOD__, \is_object($debug) ? \get_class($debug) : \gettype($debug))); | ||
} | ||
|
||
$this->serializer = $serializer; | ||
$this->format = $format; | ||
$this->fallbackErrorRenderer = $fallbackErrorRenderer; | ||
$this->debug = $debug; | ||
} | ||
|
||
public function render(\Throwable $exception): FlattenException | ||
{ | ||
$flattenException = FlattenException::createFromThrowable($exception); | ||
|
||
try { | ||
$format = is_callable($this->format) ? ($this->format)($flattenException) : $this->format; | ||
|
||
$context = new Context(); | ||
$context->setAttribute('exception', $exception); | ||
$context->setAttribute('debug', is_callable($this->debug) ? ($this->debug)($exception) : $this->debug); | ||
|
||
return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, $context)); | ||
} catch (NotEncodableValueException $e) { | ||
return $this->fallbackErrorRenderer->render($exception); | ||
} | ||
} | ||
|
||
/** | ||
* @see \Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer::getPreferredFormat | ||
*/ | ||
public static function getPreferredFormat(RequestStack $requestStack): \Closure | ||
{ | ||
return static function () use ($requestStack) { | ||
if (!$request = $requestStack->getCurrentRequest()) { | ||
throw new NotEncodableValueException(); | ||
} | ||
|
||
return $request->getPreferredFormat(); | ||
}; | ||
} | ||
|
||
/** | ||
* @see \Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer::isDebug | ||
*/ | ||
public static function isDebug(RequestStack $requestStack, bool $debug): \Closure | ||
{ | ||
return static function () use ($requestStack, $debug): bool { | ||
if (!$request = $requestStack->getCurrentRequest()) { | ||
return $debug; | ||
} | ||
|
||
return $debug && $request->attributes->getBoolean('showException', true); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\Tests\ErrorRenderer; | ||
|
||
use FOS\RestBundle\Context\Context; | ||
use FOS\RestBundle\ErrorRenderer\SerializerErrorRenderer; | ||
use FOS\RestBundle\Serializer\Serializer; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; | ||
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Serializer\Exception\NotEncodableValueException; | ||
|
||
class SerializerErrorRendererTest extends TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
if (!interface_exists(ErrorRendererInterface::class)) { | ||
$this->markTestSkipped(); | ||
} | ||
} | ||
|
||
public function testSerializeFlattenExceptionWithStringFormat() | ||
{ | ||
$serializer = $this->createMock(Serializer::class); | ||
$serializer | ||
->expects($this->once()) | ||
->method('serialize') | ||
->with($this->isInstanceOf(FlattenException::class), 'json', $this->isInstanceOf(Context::class)) | ||
->willReturn('serialized FlattenException'); | ||
|
||
$errorRenderer = new SerializerErrorRenderer($serializer, 'json'); | ||
$flattenException = $errorRenderer->render(new NotFoundHttpException()); | ||
|
||
$this->assertSame('serialized FlattenException', $flattenException->getAsString()); | ||
} | ||
|
||
public function testSerializeFlattenExceptionWithCallableFormat() | ||
{ | ||
$serializer = $this->createMock(Serializer::class); | ||
$serializer | ||
->expects($this->once()) | ||
->method('serialize') | ||
->with($this->isInstanceOf(FlattenException::class), 'json', $this->isInstanceOf(Context::class)) | ||
->willReturn('serialized FlattenException'); | ||
|
||
$format = function (FlattenException $flattenException) { | ||
return 'json'; | ||
}; | ||
|
||
$errorRenderer = new SerializerErrorRenderer($serializer, $format); | ||
$flattenException = $errorRenderer->render(new NotFoundHttpException()); | ||
|
||
$this->assertSame('serialized FlattenException', $flattenException->getAsString()); | ||
} | ||
|
||
public function testSerializeFlattenExceptionUsingGetPreferredFormatMethod() | ||
{ | ||
$serializer = $this->createMock(Serializer::class); | ||
$serializer | ||
->expects($this->once()) | ||
->method('serialize') | ||
->with($this->isInstanceOf(FlattenException::class), 'json', $this->isInstanceOf(Context::class)) | ||
->willReturn('serialized FlattenException'); | ||
|
||
$request = new Request(); | ||
$request->attributes->set('_format', 'json'); | ||
|
||
$requestStack = new RequestStack(); | ||
$requestStack->push($request); | ||
$format = SerializerErrorRenderer::getPreferredFormat($requestStack); | ||
|
||
$errorRenderer = new SerializerErrorRenderer($serializer, $format); | ||
$flattenException = $errorRenderer->render(new NotFoundHttpException()); | ||
|
||
$this->assertSame('serialized FlattenException', $flattenException->getAsString()); | ||
} | ||
|
||
public function testFallbackErrorRendererIsUsedWhenFormatCannotBeDetected() | ||
{ | ||
$exception = new NotFoundHttpException(); | ||
$flattenException = new FlattenException(); | ||
|
||
$fallbackErrorRenderer = $this->createMock(ErrorRendererInterface::class); | ||
$fallbackErrorRenderer | ||
->expects($this->once()) | ||
->method('render') | ||
->with($exception) | ||
->willReturn($flattenException); | ||
|
||
$serializer = $this->createMock(Serializer::class); | ||
$serializer->expects($this->once()) | ||
->method('serialize') | ||
->with($this->isInstanceOf(FlattenException::class), 'json', $this->isInstanceOf(Context::class)) | ||
->willThrowException(new NotEncodableValueException()); | ||
|
||
$errorRenderer = new SerializerErrorRenderer($serializer, 'json', $fallbackErrorRenderer); | ||
|
||
$this->assertSame($flattenException, $errorRenderer->render($exception)); | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
Tests/Functional/Bundle/TestBundle/ErrorRenderer/JmsSerializerErrorRenderer.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.