Catch AccountNotFoundException and show 404 page

This commit is contained in:
Lexi / Zoe 2021-09-16 21:05:15 +02:00
parent 930726432e
commit 19b8075e3c
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
2 changed files with 41 additions and 5 deletions

View File

@ -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);

View File

@ -0,0 +1,14 @@
{% extends "base.html.twig" %}
{% block title %}Account not found{% endblock %}
{% block content %}
<h2>Account not found</h2>
<div class="error_box">
<h4>Error</h4>
<p>The account with the ID {{ id }} was not found.</p>
</div>
<p>Go back to <a href="/accounts">account list</a>.</p>
{% endblock %}