Skip to content

Commit 0afe2d6

Browse files
authored
Handle weblink not found error (#565)
* Handle weblink not found error * CS
1 parent af805d0 commit 0afe2d6

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

src/administrator/components/com_weblinks/src/View/Weblink/HtmlView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected function addToolbar()
157157

158158
ToolbarHelper::cancel('weblink.cancel', 'JTOOLBAR_CLOSE');
159159

160-
if (ComponentHelper::isEnabled('com_contenthistory') && $this->state->params->get('save_history', 0) && $itemEditable) {
160+
if (ComponentHelper::isEnabled('com_contenthistory') && $this->state->get('params')->get('save_history', 0) && $itemEditable) {
161161
ToolbarHelper::versions('com_weblinks.weblink', $this->item->id);
162162
}
163163

src/administrator/components/com_weblinks/src/View/Weblinks/HtmlView.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
// phpcs:disable PSR1.Files.SideEffects
1414
\defined('_JEXEC') or die;
15+
1516
// phpcs:enable PSR1.Files.SideEffects
1617

1718
use Joomla\CMS\Factory;
1819
use Joomla\CMS\Helper\ContentHelper;
1920
use Joomla\CMS\Language\Text;
2021
use Joomla\CMS\MVC\View\GenericDataException;
2122
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
22-
use Joomla\CMS\Toolbar\Toolbar;
2323
use Joomla\CMS\Toolbar\ToolbarHelper;
2424
use Joomla\Component\Weblinks\Administrator\Model\WeblinksModel;
2525

@@ -66,11 +66,11 @@ class HtmlView extends BaseHtmlView
6666
public $activeFilters;
6767

6868
/**
69-
* Is this view an Empty State
70-
*
71-
* @var boolean
72-
* @since 4.0.0
73-
*/
69+
* Is this view an Empty State
70+
*
71+
* @var boolean
72+
* @since 4.0.0
73+
*/
7474
private $isEmptyState = false;
7575

7676
/**
@@ -92,7 +92,7 @@ public function display($tpl = null)
9292
$this->activeFilters = $model->getActiveFilters();
9393

9494
// Check for errors.
95-
if (\count($errors = $this->get('Errors'))) {
95+
if (\count($errors = $model->getErrors())) {
9696
throw new GenericDataException(implode("\n", $errors), 500);
9797
}
9898

src/components/com_weblinks/src/Model/WeblinkModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function getItem($pk = null)
163163
}
164164

165165
// Compute access permissions.
166-
if ($access = $this->getState('filter.access')) {
166+
if ($this->getState('filter.access')) {
167167
// If the access filter has been set, we already know this user can view.
168168
$data->params->set('access-view', true);
169169
} else {

src/components/com_weblinks/src/View/Weblink/HtmlView.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Joomla\Component\Weblinks\Site\View\Weblink;
1212

1313
use Joomla\CMS\Factory;
14+
use Joomla\CMS\MVC\View\GenericDataException;
1415
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
1516
use Joomla\Component\Weblinks\Site\Model\WeblinkModel;
1617

@@ -54,17 +55,25 @@ class HtmlView extends BaseHtmlView
5455
*
5556
* @return mixed A string if successful, otherwise an Error object.
5657
*
58+
* @throws \Exception
5759
* @since __DEPLOY_VERSION__
5860
*/
5961
public function display($tpl = null)
6062
{
61-
$app = Factory::getApplication();
63+
$app = Factory::getApplication();
6264

63-
/* @var WeblinkModel $model*/
65+
/* @var WeblinkModel $model */
6466
$model = $this->getModel();
6567
$this->item = $model->getItem();
6668
$this->state = $model->getState();
6769
$this->params = $this->state->get('params');
70+
71+
$errors = $model->getErrors();
72+
73+
if (\count($errors) > 0) {
74+
$this->handleModelErrors($errors);
75+
}
76+
6877
// Create a shortcut for $item.
6978
$item = $this->item;
7079
$item->slug = $item->alias ? ($item->id . ':' . $item->alias) : $item->id;
@@ -82,4 +91,25 @@ public function display($tpl = null)
8291
$item->event->afterDisplayContent = trim(implode("\n", $results));
8392
parent::display($tpl);
8493
}
94+
95+
/**
96+
* Handle errors returned by model
97+
*
98+
* @param array $errors
99+
*
100+
* @return void
101+
* @throws \Exception
102+
*/
103+
private function handleModelErrors(array $errors): void
104+
{
105+
foreach ($errors as $error) {
106+
// Throws 404 error if weblink item not found
107+
if ($error instanceof \Exception && $error->getCode() === 404) {
108+
throw $error;
109+
}
110+
}
111+
112+
// Otherwise, it is database runtime error, and we will throw error 500
113+
throw new GenericDataException(implode("\n", $errors), 500);
114+
}
85115
}

0 commit comments

Comments
 (0)