-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add missing error normalizer trait and remove deprecated interfa…
…ce (#6853)
- Loading branch information
1 parent
4defee0
commit 33f79ab
Showing
2 changed files
with
53 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\JsonApi\Serializer; | ||
|
||
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
trait ErrorNormalizerTrait | ||
{ | ||
private function getErrorMessage($object, array $context, bool $debug = false): string | ||
{ | ||
$message = $object->getMessage(); | ||
|
||
if ($debug) { | ||
return $message; | ||
} | ||
|
||
if ($object instanceof FlattenException) { | ||
$statusCode = $context['statusCode'] ?? $object->getStatusCode(); | ||
if ($statusCode >= 500 && $statusCode < 600) { | ||
$message = Response::$statusTexts[$statusCode] ?? Response::$statusTexts[Response::HTTP_INTERNAL_SERVER_ERROR]; | ||
} | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
private function getErrorCode(object $object): ?string | ||
{ | ||
if ($object instanceof FlattenException) { | ||
return (string)$object->getStatusCode(); | ||
} | ||
|
||
if ($object instanceof \Exception) { | ||
$code = $object->getCode(); | ||
return $code !== 0 ? (string)$code : null; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|