14
14
use ApiPlatform \Core \Api \UrlGeneratorInterface ;
15
15
use ApiPlatform \Core \Hydra \Serializer \ErrorNormalizer ;
16
16
use Symfony \Component \Debug \Exception \FlattenException ;
17
+ use Symfony \Component \HttpFoundation \Response ;
17
18
18
19
/**
19
20
* @author Kévin Dunglas <[email protected] >
@@ -35,6 +36,47 @@ public function testSupportNormalization()
35
36
$ this ->assertFalse ($ normalizer ->supportsNormalization (new \stdClass (), ErrorNormalizer::FORMAT ));
36
37
}
37
38
39
+ /**
40
+ * @dataProvider providerStatusCode
41
+ *
42
+ * @param $status http status code of the Exception
43
+ * @param $originalMessage original message of the Exception
44
+ * @param $debug simulates kernel debug variable
45
+ */
46
+ public function testErrorServerNormalize ($ status , $ originalMessage , $ debug )
47
+ {
48
+ $ urlGeneratorProphecy = $ this ->prophesize (UrlGeneratorInterface::class);
49
+ $ urlGeneratorProphecy ->generate ('api_jsonld_context ' , ['shortName ' => 'Error ' ])->willReturn ('/context/foo ' )->shouldBeCalled ();
50
+
51
+ $ normalizer = new ErrorNormalizer ($ urlGeneratorProphecy ->reveal (), $ debug );
52
+ $ exception = FlattenException::create (new \Exception ($ originalMessage ), $ status );
53
+
54
+ $ expected = [
55
+ '@context ' => '/context/foo ' ,
56
+ '@type ' => 'hydra:Error ' ,
57
+ 'hydra:title ' => 'An error occurred ' ,
58
+ 'hydra:description ' => ($ debug || $ status < 500 ) ? $ originalMessage : Response::$ statusTexts [$ status ],
59
+ ];
60
+
61
+ if ($ debug ) {
62
+ $ expected ['trace ' ] = $ exception ->getTrace ();
63
+ }
64
+
65
+ $ this ->assertEquals ($ expected , $ normalizer ->normalize ($ exception , null , ['statusCode ' => $ status ]));
66
+ }
67
+
68
+ public function providerStatusCode ()
69
+ {
70
+ return [
71
+ [Response::HTTP_INTERNAL_SERVER_ERROR , 'Sensitive SQL error displayed ' , false ],
72
+ [Response::HTTP_GATEWAY_TIMEOUT , 'Sensitive server error displayed ' , false ],
73
+ [Response::HTTP_BAD_REQUEST , 'Bad Request Message ' , false ],
74
+ [Response::HTTP_INTERNAL_SERVER_ERROR , 'Sensitive SQL error displayed ' , true ],
75
+ [Response::HTTP_GATEWAY_TIMEOUT , 'Sensitive server error displayed ' , true ],
76
+ [Response::HTTP_BAD_REQUEST , 'Bad Request Message ' , true ],
77
+ ];
78
+ }
79
+
38
80
public function testNormalize ()
39
81
{
40
82
$ urlGeneratorProphecy = $ this ->prophesize (UrlGeneratorInterface::class);
0 commit comments