Skip to content

Commit 0c9f1eb

Browse files
committed
feat(symfony): Improve error handling in CatchAllController
Enhance error handling for DotCMS API interactions: - Add specific exception mapping for HTTP errors - Translate DotCMS exceptions to appropriate Symfony HTTP exceptions - Provide more granular error responses for different API error scenarios - Improve error logging and client-side error presentation
1 parent 0705c4f commit 0c9f1eb

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
77
use Symfony\Component\HttpFoundation\Response;
88
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9+
use Dotcms\PhpSdk\Exception\DotCMSException;
10+
use Dotcms\PhpSdk\Exception\HttpException;
11+
use Dotcms\PhpSdk\Exception\ResponseException;
12+
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
13+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
14+
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
915

1016
class CatchAllController extends AbstractController
1117
{
@@ -33,8 +39,25 @@ public function show(string $path = ''): Response
3339
'page' => $pageAsset->page ?? null,
3440
'containers' => $pageAsset->containers ?? []
3541
]);
42+
} catch (HttpException $e) {
43+
// Map HTTP errors to appropriate Symfony exceptions
44+
$statusCode = $e->getCode();
45+
throw match($statusCode) {
46+
401 => new UnauthorizedHttpException('Bearer', $e->getMessage(), $e),
47+
404 => new NotFoundHttpException($e->getMessage(), $e),
48+
400 => new BadRequestHttpException($e->getMessage(), $e),
49+
503 => new ServiceUnavailableHttpException(null, $e->getMessage(), $e),
50+
default => new NotFoundHttpException($e->getMessage(), $e)
51+
};
52+
} 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);
3658
} catch (\Exception $e) {
37-
throw new NotFoundHttpException($e->getMessage(), $e);
59+
// Handle any other unexpected errors
60+
throw new ServiceUnavailableHttpException(null, 'Unexpected error: ' . $e->getMessage(), $e);
3861
}
3962
}
4063
}

0 commit comments

Comments
 (0)