Skip to content
40 changes: 20 additions & 20 deletions administrator/components/com_content/src/View/Articles/HtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,28 @@ public function display($tpl = null)
{
/** @var ArticlesModel $model */
$model = $this->getModel();
$model->setUseExceptions(true);

try {
$this->items = $model->getItems();
$this->pagination = $model->getPagination();
$this->state = $model->getState();
$this->filterForm = $model->getFilterForm();
$this->activeFilters = $model->getActiveFilters();
$this->vote = PluginHelper::isEnabled('content', 'vote');
$this->hits = ComponentHelper::getParams('com_content')->get('record_hits', 1) == 1;

if (!\count($this->items) && $this->isEmptyState = $model->getIsEmptyState()) {
$this->setLayout('emptystate');
}

$this->items = $model->getItems();
$this->pagination = $model->getPagination();
$this->state = $model->getState();
$this->filterForm = $model->getFilterForm();
$this->activeFilters = $model->getActiveFilters();
$this->vote = PluginHelper::isEnabled('content', 'vote');
$this->hits = ComponentHelper::getParams('com_content')->get('record_hits', 1) == 1;

if (!\count($this->items) && $this->isEmptyState = $model->getIsEmptyState()) {
$this->setLayout('emptystate');
}

if (ComponentHelper::getParams('com_content')->get('workflow_enabled')) {
PluginHelper::importPlugin('workflow');

$this->transitions = $model->getTransitions();
}
if (ComponentHelper::getParams('com_content')->get('workflow_enabled')) {
PluginHelper::importPlugin('workflow');

// Check for errors.
if (\count($errors = $model->getErrors()) || $this->transitions === false) {
throw new GenericDataException(implode("\n", $errors), 500);
$this->transitions = $model->getTransitions();
}
} catch (\Exception $e) {
throw new GenericDataException($e->getMessage(), 500, $e);
}

// We don't need toolbar in the modal window.
Expand Down
4 changes: 4 additions & 0 deletions libraries/src/MVC/Model/BaseDatabaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public function getTable($name = '', $prefix = '', $options = [])
}

if ($table = $this->_createTable($name, $prefix, $options)) {
if ($this->shouldUseExceptions()) {
$table->setUseExceptions(true);
}

return $table;
}

Expand Down
49 changes: 45 additions & 4 deletions libraries/src/Object/LegacyErrorHandlingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @since 4.3.0
*
* @deprecated 4.3 will be removed in 6.0
* @deprecated 4.3 will be removed in 7.0
* Will be removed without replacement
* Throw an Exception instead of setError
*/
Expand All @@ -36,6 +36,15 @@ trait LegacyErrorHandlingTrait
protected $_errors = [];
// phpcs:enable PSR2.Classes.PropertyDeclaration

/**
* Use exceptions rather than getError/setError.
*
* @var boolean
* @since __DEPLOY_VERSION__
* @deprecated 7.0
*/
private bool $useExceptions = false;

/**
* Get the most recent error message.
*
Expand All @@ -46,7 +55,7 @@ trait LegacyErrorHandlingTrait
*
* @since 1.7.0
*
* @deprecated 3.1.4 will be removed in 6.0
* @deprecated 3.1.4 will be removed in 7.0
* Will be removed without replacement
* Catch thrown Exceptions instead of getError
*/
Expand Down Expand Up @@ -78,7 +87,7 @@ public function getError($i = null, $toString = true)
*
* @since 1.7.0
*
* @deprecated 3.1.4 will be removed in 6.0
* @deprecated 3.1.4 will be removed in 7.0
* Will be removed without replacement
* Catch thrown Exceptions instead of getErrors
*/
Expand All @@ -96,12 +105,44 @@ public function getErrors()
*
* @since 1.7.0
*
* @deprecated 3.1.4 will be removed in 6.0
* @deprecated 3.1.4 will be removed in 7.0
* Will be removed without replacement
* Throw an Exception instead of using setError
*/
public function setError($error)
{
if ($this->useExceptions && \is_string($error)) {
throw new \Exception($error, 500);
}

$this->_errors[] = $error;
}

/**
* If true then subclasses should throw exceptions rather than use getError and setError.
*
* @return boolean
*
* @since __DEPLOY_VERSION__
* @deprecated 7.0
*/
public function shouldUseExceptions(): bool
{
return $this->useExceptions;
}

/**
* If true then subclasses should throw exceptions rather than use getError and setError.
*
* @param boolean $value The value to set for the field.
*
* @return void
*
* @since __DEPLOY_VERSION__
* @deprecated 7.0
*/
public function setUseExceptions(bool $value): void
{
$this->useExceptions = $value;
}
}
10 changes: 10 additions & 0 deletions tests/Unit/Libraries/Cms/MVC/Model/FormModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function testFailedCheckin()
$table->method('hasField')->willReturn(true);
$table->method('checkIn')->willReturn(false);
$table->method('getColumnAlias')->willReturn('checked_out');
$table->method('getError')->willReturn('ERROR MESSAGE');

$mvcFactory = $this->createStub(MVCFactoryInterface::class);
$mvcFactory->method('createTable')->willReturn($table);
Expand All @@ -106,6 +107,10 @@ public function getForm($data = [], $loadData = true)
$model->setCurrentUser(new User());

$this->assertFalse($model->checkin(1));

$this->expectException(\Exception::class);
$model->setUseExceptions(true);
$model->checkin(1);
}

/**
Expand Down Expand Up @@ -298,6 +303,7 @@ public function testFailedCheckout()
$table->method('hasField')->willReturn(true);
$table->method('checkIn')->willReturn(false);
$table->method('getColumnAlias')->willReturn('checked_out');
$table->method('getError')->willReturn('ERROR MESSAGE');

$mvcFactory = $this->createStub(MVCFactoryInterface::class);
$mvcFactory->method('createTable')->willReturn($table);
Expand All @@ -315,6 +321,10 @@ public function getForm($data = [], $loadData = true)
$model->setCurrentUser($user);

$this->assertFalse($model->checkout(1));

$this->expectException(\Exception::class);
$model->setUseExceptions(true);
$model->checkout(1);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Libraries/Cms/Object/CMSObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function testGetProperties()
'_privateproperty1' => 'valuep1',
'property1' => 'value1',
'property2' => 5,
'useExceptions' => false,
],
$object->getProperties(false),
'Should get all properties, including private ones'
Expand Down