File tree Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 1313
1414namespace ApiPlatform \JsonApi \Serializer ;
1515
16- use ApiPlatform \Problem \Serializer \ErrorNormalizerTrait ;
1716use ApiPlatform \Serializer \CacheableSupportsMethodInterface ;
1817use ApiPlatform \Symfony \Validator \Exception \ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface ;
1918use ApiPlatform \Validator \Exception \ConstraintViolationListAwareExceptionInterface ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the API Platform project.
5+ *
6+ * (c) Kévin Dunglas <dunglas@gmail.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+ declare (strict_types=1 );
13+
14+ namespace ApiPlatform \JsonApi \Serializer ;
15+
16+ use Symfony \Component \ErrorHandler \Exception \FlattenException ;
17+ use Symfony \Component \HttpFoundation \Response ;
18+
19+ trait ErrorNormalizerTrait
20+ {
21+ private function getErrorMessage ($ object , array $ context , bool $ debug = false ): string
22+ {
23+ $ message = $ object ->getMessage ();
24+
25+ if ($ debug ) {
26+ return $ message ;
27+ }
28+
29+ if ($ object instanceof FlattenException) {
30+ $ statusCode = $ context ['statusCode ' ] ?? $ object ->getStatusCode ();
31+ if ($ statusCode >= 500 && $ statusCode < 600 ) {
32+ $ message = Response::$ statusTexts [$ statusCode ] ?? Response::$ statusTexts [Response::HTTP_INTERNAL_SERVER_ERROR ];
33+ }
34+ }
35+
36+ return $ message ;
37+ }
38+
39+ private function getErrorCode (object $ object ): ?string
40+ {
41+ if ($ object instanceof FlattenException) {
42+ return (string )$ object ->getStatusCode ();
43+ }
44+
45+ if ($ object instanceof \Exception) {
46+ $ code = $ object ->getCode ();
47+ return $ code !== 0 ? (string )$ code : null ;
48+ }
49+
50+ return null ;
51+ }
52+ }
53+
You can’t perform that action at this time.
0 commit comments