File tree Expand file tree Collapse file tree 5 files changed +73
-0
lines changed
app/CustomFlattenExceptionNormalizer Expand file tree Collapse file tree 5 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1616use Symfony \Component \Form \Extension \Core \Type \TextType ;
1717use Symfony \Component \Validator \Constraints \NotBlank ;
1818
19+ class CustomArgumentException extends \Exception
20+ {
21+ }
22+
1923/**
2024 * Controller to test serialization of various errors and exceptions.
2125 *
@@ -38,6 +42,11 @@ public function invalidArgumentExceptionAction()
3842 throw new \InvalidArgumentException ('Invalid argument given. ' );
3943 }
4044
45+ public function customExceptionAction ()
46+ {
47+ throw new CustomArgumentException ('Custom exception ' );
48+ }
49+
4150 /**
4251 * @View
4352 */
Original file line number Diff line number Diff line change @@ -18,6 +18,10 @@ test_serializer_error_invalid_form:
1818 path : /serializer-error/invalid-form.{_format}
1919 defaults : { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\SerializerErrorController::invalidFormAction }
2020
21+ test_custom_exception_serializer :
22+ path : /serializer-error/custom-argument-exception.{_format}
23+ defaults : { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\SerializerErrorController::customExceptionAction }
24+
2125# Must be defined before test_version
2226test_version2 :
2327 resource : FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Version2Controller
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ class SerializerErrorTest extends WebTestCase
2222{
2323 public static function tearDownAfterClass (): void
2424 {
25+ self ::deleteTmpDir ('CustomFlattenExceptionNormalizer ' );
2526 self ::deleteTmpDir ('FlattenExceptionHandlerLegacyFormat ' );
2627 self ::deleteTmpDir ('FlattenExceptionHandlerRfc7807Format ' );
2728 self ::deleteTmpDir ('FlattenExceptionNormalizerLegacyFormat ' );
@@ -121,6 +122,23 @@ public function testSerializeExceptionCodeMappedToResponseStatusCodeJsonUsingErr
121122 $ this ->assertEquals (json_encode ($ expectedJson ), $ client ->getResponse ()->getContent ());
122123 }
123124
125+ public function testCustomExceptionSerialization ()
126+ {
127+ if (!class_exists (SerializerErrorRenderer::class)) {
128+ $ this ->markTestSkipped ();
129+ }
130+
131+ $ this ->iniSet ('error_log ' , file_exists ('/dev/null ' ) ? '/dev/null ' : 'nul ' );
132+
133+ $ client = $ this ->createClient (['test_case ' => 'CustomFlattenExceptionNormalizer ' , 'debug ' => false ]);
134+ $ client ->request ('GET ' , '/serializer-error/custom-argument-exception.json ' );
135+
136+ $ this ->assertEquals (
137+ '{"code":409,"message":"Conflict"} ' ,
138+ $ client ->getResponse ()->getContent ()
139+ );
140+ }
141+
124142 public function serializeExceptionCodeMappedToResponseStatusCodeJsonProvider (): array
125143 {
126144 return [
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the FOSRestBundle package.
5+ *
6+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ return [
13+ new \Symfony \Bundle \FrameworkBundle \FrameworkBundle (),
14+ new \FOS \RestBundle \FOSRestBundle (),
15+ new \JMS \SerializerBundle \JMSSerializerBundle (),
16+ new \FOS \RestBundle \Tests \Functional \Bundle \TestBundle \TestBundle (),
17+ ];
Original file line number Diff line number Diff line change 1+ imports :
2+ - { resource: ../config/default.yml }
3+
4+ framework :
5+ serializer : true
6+
7+ fos_rest :
8+ exception :
9+ codes :
10+ ' FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\CustomArgumentException ' : 409
11+ enabled : true
12+ map_exception_codes : true
13+ exception_listener : false
14+ serialize_exceptions : false
15+ flatten_exception_format : ' legacy'
16+ serializer_error_renderer : false
17+ serializer :
18+ serialize_null : true
19+ body_listener :
20+ enabled : true
21+ routing_loader : false
22+ view :
23+ formats :
24+ json : true
25+ csv : true
You can’t perform that action at this time.
0 commit comments