diff --git a/src/Frontend/Accounts/AccountController.php b/src/Frontend/Accounts/AccountController.php index 7209067..4a6e268 100644 --- a/src/Frontend/Accounts/AccountController.php +++ b/src/Frontend/Accounts/AccountController.php @@ -6,6 +6,7 @@ namespace MailAccountAdmin\Frontend\Accounts; use MailAccountAdmin\Common\ActionResult; use MailAccountAdmin\Common\SessionHelper; use MailAccountAdmin\Common\UserHelper; +use MailAccountAdmin\Exceptions\AccountNotFoundException; use MailAccountAdmin\Exceptions\AppException; use MailAccountAdmin\Exceptions\InputValidationError; use MailAccountAdmin\Frontend\BaseController; @@ -24,6 +25,14 @@ class AccountController extends BaseController } + // -- Error pages + + public function showAccount404(Response $response, int $accountId): Response + { + return $this->view->render($response, 'account_404.html.twig', ['id' => $accountId])->withStatus(404); + } + + // -- /accounts - List all accounts public function showAccounts(Request $request, Response $response): Response @@ -49,7 +58,12 @@ class AccountController extends BaseController // Parse URL arguments $accountId = (int)$args['id']; - $renderData = $this->accountHandler->getAccountDetails($accountId); + try { + $renderData = $this->accountHandler->getAccountDetails($accountId); + } catch (AccountNotFoundException $e) { + return $this->showAccount404($response, $accountId); + } + return $this->view->render($response, 'account_details.html.twig', $renderData); } @@ -99,8 +113,12 @@ class AccountController extends BaseController // Parse URL arguments $accountId = (int)$args['id']; - // Get account data from database - $renderData = $this->accountHandler->getAccountDataForEdit($accountId); + try { + // Get account data from database + $renderData = $this->accountHandler->getAccountDataForEdit($accountId); + } catch (AccountNotFoundException $e) { + return $this->showAccount404($response, $accountId); + } // If the form has been submitted, add the result message and form input data to the render data array $renderData = $this->addLastActionResultToRenderData($renderData); @@ -142,8 +160,12 @@ class AccountController extends BaseController // Parse URL arguments $accountId = (int)$args['id']; - // Get account data and list of aliases from database - $renderData = $this->accountHandler->getAccountDataForDelete($accountId); + try { + // Get account data and list of aliases from database + $renderData = $this->accountHandler->getAccountDataForDelete($accountId); + } catch (AccountNotFoundException $e) { + return $this->showAccount404($response, $accountId); + } // If the form has been submitted, add the result message to the render data array $renderData = $this->addLastActionResultToRenderData($renderData); diff --git a/templates/account_404.html.twig b/templates/account_404.html.twig new file mode 100644 index 0000000..5c17302 --- /dev/null +++ b/templates/account_404.html.twig @@ -0,0 +1,14 @@ +{% extends "base.html.twig" %} + +{% block title %}Account not found{% endblock %} + +{% block content %} +

Account not found

+ +
+

Error

+

The account with the ID {{ id }} was not found.

+
+ +

Go back to account list.

+{% endblock %}