Skip to content

Commit

Permalink
fix: add missing error normalizer trait and remove deprecated interfa…
Browse files Browse the repository at this point in the history
…ce (#6853)
  • Loading branch information
mladencosa authored Dec 9, 2024
1 parent 4defee0 commit 33f79ab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
1 change: 0 additions & 1 deletion Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\JsonApi\Serializer;

use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
Expand Down
53 changes: 53 additions & 0 deletions Serializer/ErrorNormalizerTrait.php
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;
}
}

0 comments on commit 33f79ab

Please sign in to comment.