+ */
+class ProblemNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
+{
+ private $debug;
+ private $defaultContext = [
+ 'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
+ 'title' => 'An error occurred',
+ ];
+
+ public function __construct(bool $debug = false, array $defaultContext = [])
+ {
+ $this->debug = $debug;
+ $this->defaultContext = $defaultContext + $this->defaultContext;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function normalize($exception, $format = null, array $context = [])
+ {
+ if (!$exception instanceof FlattenException) {
+ $exception = FlattenException::createFromThrowable($exception);
+ }
+
+ $context += $this->defaultContext;
+ $debug = $this->debug && ($context['debug'] ?? true);
+
+ $data = [
+ 'type' => $context['type'],
+ 'title' => $context['title'],
+ 'status' => $context['status'] ?? $exception->getStatusCode(),
+ 'detail' => $debug ? $exception->getMessage() : $exception->getStatusText(),
+ ];
+ if ($debug) {
+ $data['class'] = $exception->getClass();
+ $data['trace'] = $exception->getTrace();
+ }
+
+ return $data;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function supportsNormalization($data, $format = null): bool
+ {
+ return $data instanceof \Exception || $data instanceof FlattenException;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function hasCacheableSupportsMethod(): bool
+ {
+ return true;
+ }
+}
diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ProblemNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ProblemNormalizerTest.php
new file mode 100644
index 0000000000000..409fbb5ffd98b
--- /dev/null
+++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ProblemNormalizerTest.php
@@ -0,0 +1,48 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Serializer\Tests\Normalizer;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\ErrorRenderer\Exception\FlattenException;
+use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
+
+class ProblemNormalizerTest extends TestCase
+{
+ /**
+ * @var ProblemNormalizer
+ */
+ private $normalizer;
+
+ protected function setUp(): void
+ {
+ $this->normalizer = new ProblemNormalizer(false);
+ }
+
+ public function testSupportNormalization()
+ {
+ $this->assertTrue($this->normalizer->supportsNormalization(new \Exception()));
+ $this->assertTrue($this->normalizer->supportsNormalization(FlattenException::createFromThrowable(new \Exception())));
+ $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
+ }
+
+ public function testNormalize()
+ {
+ $expected = [
+ 'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
+ 'title' => 'An error occurred',
+ 'status' => 500,
+ 'detail' => 'Internal Server Error',
+ ];
+
+ $this->assertSame($expected, $this->normalizer->normalize(new \RuntimeException('Error')));
+ }
+}
diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json
index 45c185a364e7e..da613fd62895a 100644
--- a/src/Symfony/Component/Serializer/composer.json
+++ b/src/Symfony/Component/Serializer/composer.json
@@ -20,6 +20,7 @@
"symfony/polyfill-ctype": "~1.8"
},
"require-dev": {
+ "symfony/error-renderer": "^4.4|^5.0",
"symfony/yaml": "^3.4|^4.0|^5.0",
"symfony/config": "^3.4|^4.0|^5.0",
"symfony/property-access": "^3.4|^4.0|^5.0",
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy