Skip to content

Commit 6a6e01b

Browse files
committed
refactor(symfony): Streamline error handling in CatchAllController
Optimize exception mapping and error response strategy: - Simplify HTTP error handling with match expression - Add generic 500 error handling for unexpected server errors - Remove redundant exception catch blocks - Improve default error response for unhandled scenarios
1 parent 0c9f1eb commit 6a6e01b

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

examples/dotcms-symfony/src/Controller/CatchAllController.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use App\Service\DotCMSService;
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
77
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
89
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9-
use Dotcms\PhpSdk\Exception\DotCMSException;
10-
use Dotcms\PhpSdk\Exception\HttpException;
11-
use Dotcms\PhpSdk\Exception\ResponseException;
1210
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
13-
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1411
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
12+
use Symfony\Component\HttpKernel\Exception\HttpException as SymfonyHttpException;
13+
use Dotcms\PhpSdk\Exception\HttpException;
14+
use Dotcms\PhpSdk\Exception\ResponseException;
1515

1616
class CatchAllController extends AbstractController
1717
{
@@ -41,23 +41,18 @@ public function show(string $path = ''): Response
4141
]);
4242
} catch (HttpException $e) {
4343
// Map HTTP errors to appropriate Symfony exceptions
44-
$statusCode = $e->getCode();
45-
throw match($statusCode) {
44+
throw match($e->getCode()) {
45+
400 => new BadRequestHttpException($e->getMessage(), $e),
4646
401 => new UnauthorizedHttpException('Bearer', $e->getMessage(), $e),
4747
404 => new NotFoundHttpException($e->getMessage(), $e),
48-
400 => new BadRequestHttpException($e->getMessage(), $e),
48+
500 => new SymfonyHttpException(500, $e->getMessage(), $e),
4949
503 => new ServiceUnavailableHttpException(null, $e->getMessage(), $e),
50-
default => new NotFoundHttpException($e->getMessage(), $e)
50+
default => new ServiceUnavailableHttpException(null, $e->getMessage(), $e)
5151
};
5252
} catch (ResponseException $e) {
53-
// Handle response parsing errors
54-
throw new BadRequestHttpException('Invalid response from DotCMS: ' . $e->getMessage(), $e);
55-
} catch (DotCMSException $e) {
56-
// Handle any other DotCMS specific errors
57-
throw new ServiceUnavailableHttpException(null, 'DotCMS Error: ' . $e->getMessage(), $e);
58-
} catch (\Exception $e) {
59-
// Handle any other unexpected errors
60-
throw new ServiceUnavailableHttpException(null, 'Unexpected error: ' . $e->getMessage(), $e);
53+
// ResponseException indicates invalid/missing data in the response
54+
// This is a server error since the response format is controlled by DotCMS
55+
throw new ServiceUnavailableHttpException(null, $e->getMessage(), $e);
6156
}
6257
}
6358
}

0 commit comments

Comments
 (0)