Skip to content

Commit 35bf0b4

Browse files
authored
[5.4] Error handling: Adding new shouldUseException() (#44098)
To make an easy transition from the legacy error handling with "getError" and "setError" methods, this PR introduces new methods "shouldUseExceptions" and "setUseExceptions" to the "LegacyErrorHandlingTrait". The latter allows to set a flag if exceptions should be used or not, and the former allows to check if that flag is set or not. The plan is to add this to 5.4 and then to remove all of this from 7.0 again.
1 parent fb908ba commit 35bf0b4

File tree

5 files changed

+80
-24
lines changed

5 files changed

+80
-24
lines changed

administrator/components/com_content/src/View/Articles/HtmlView.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,28 @@ public function display($tpl = null)
111111
{
112112
/** @var ArticlesModel $model */
113113
$model = $this->getModel();
114+
$model->setUseExceptions(true);
115+
116+
try {
117+
$this->items = $model->getItems();
118+
$this->pagination = $model->getPagination();
119+
$this->state = $model->getState();
120+
$this->filterForm = $model->getFilterForm();
121+
$this->activeFilters = $model->getActiveFilters();
122+
$this->vote = PluginHelper::isEnabled('content', 'vote');
123+
$this->hits = ComponentHelper::getParams('com_content')->get('record_hits', 1) == 1;
124+
125+
if (!\count($this->items) && $this->isEmptyState = $model->getIsEmptyState()) {
126+
$this->setLayout('emptystate');
127+
}
114128

115-
$this->items = $model->getItems();
116-
$this->pagination = $model->getPagination();
117-
$this->state = $model->getState();
118-
$this->filterForm = $model->getFilterForm();
119-
$this->activeFilters = $model->getActiveFilters();
120-
$this->vote = PluginHelper::isEnabled('content', 'vote');
121-
$this->hits = ComponentHelper::getParams('com_content')->get('record_hits', 1) == 1;
122-
123-
if (!\count($this->items) && $this->isEmptyState = $model->getIsEmptyState()) {
124-
$this->setLayout('emptystate');
125-
}
126-
127-
if (ComponentHelper::getParams('com_content')->get('workflow_enabled')) {
128-
PluginHelper::importPlugin('workflow');
129-
130-
$this->transitions = $model->getTransitions();
131-
}
129+
if (ComponentHelper::getParams('com_content')->get('workflow_enabled')) {
130+
PluginHelper::importPlugin('workflow');
132131

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

138138
// We don't need toolbar in the modal window.

libraries/src/MVC/Model/BaseDatabaseModel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ public function getTable($name = '', $prefix = '', $options = [])
263263
}
264264

265265
if ($table = $this->_createTable($name, $prefix, $options)) {
266+
if ($this->shouldUseExceptions()) {
267+
$table->setUseExceptions(true);
268+
}
269+
266270
return $table;
267271
}
268272

libraries/src/Object/LegacyErrorHandlingTrait.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @since 4.3.0
2121
*
22-
* @deprecated 4.3 will be removed in 6.0
22+
* @deprecated 4.3 will be removed in 7.0
2323
* Will be removed without replacement
2424
* Throw an Exception instead of setError
2525
*/
@@ -36,6 +36,15 @@ trait LegacyErrorHandlingTrait
3636
protected $_errors = [];
3737
// phpcs:enable PSR2.Classes.PropertyDeclaration
3838

39+
/**
40+
* Use exceptions rather than getError/setError.
41+
*
42+
* @var boolean
43+
* @since __DEPLOY_VERSION__
44+
* @deprecated 7.0
45+
*/
46+
private bool $useExceptions = false;
47+
3948
/**
4049
* Get the most recent error message.
4150
*
@@ -46,7 +55,7 @@ trait LegacyErrorHandlingTrait
4655
*
4756
* @since 1.7.0
4857
*
49-
* @deprecated 3.1.4 will be removed in 6.0
58+
* @deprecated 3.1.4 will be removed in 7.0
5059
* Will be removed without replacement
5160
* Catch thrown Exceptions instead of getError
5261
*/
@@ -78,7 +87,7 @@ public function getError($i = null, $toString = true)
7887
*
7988
* @since 1.7.0
8089
*
81-
* @deprecated 3.1.4 will be removed in 6.0
90+
* @deprecated 3.1.4 will be removed in 7.0
8291
* Will be removed without replacement
8392
* Catch thrown Exceptions instead of getErrors
8493
*/
@@ -96,12 +105,44 @@ public function getErrors()
96105
*
97106
* @since 1.7.0
98107
*
99-
* @deprecated 3.1.4 will be removed in 6.0
108+
* @deprecated 3.1.4 will be removed in 7.0
100109
* Will be removed without replacement
101110
* Throw an Exception instead of using setError
102111
*/
103112
public function setError($error)
104113
{
114+
if ($this->useExceptions && \is_string($error)) {
115+
throw new \Exception($error, 500);
116+
}
117+
105118
$this->_errors[] = $error;
106119
}
120+
121+
/**
122+
* If true then subclasses should throw exceptions rather than use getError and setError.
123+
*
124+
* @return boolean
125+
*
126+
* @since __DEPLOY_VERSION__
127+
* @deprecated 7.0
128+
*/
129+
public function shouldUseExceptions(): bool
130+
{
131+
return $this->useExceptions;
132+
}
133+
134+
/**
135+
* If true then subclasses should throw exceptions rather than use getError and setError.
136+
*
137+
* @param boolean $value The value to set for the field.
138+
*
139+
* @return void
140+
*
141+
* @since __DEPLOY_VERSION__
142+
* @deprecated 7.0
143+
*/
144+
public function setUseExceptions(bool $value): void
145+
{
146+
$this->useExceptions = $value;
147+
}
107148
}

tests/Unit/Libraries/Cms/MVC/Model/FormModelTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function testFailedCheckin()
9393
$table->method('hasField')->willReturn(true);
9494
$table->method('checkIn')->willReturn(false);
9595
$table->method('getColumnAlias')->willReturn('checked_out');
96+
$table->method('getError')->willReturn('ERROR MESSAGE');
9697

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

108109
$this->assertFalse($model->checkin(1));
110+
111+
$this->expectException(\Exception::class);
112+
$model->setUseExceptions(true);
113+
$model->checkin(1);
109114
}
110115

111116
/**
@@ -298,6 +303,7 @@ public function testFailedCheckout()
298303
$table->method('hasField')->willReturn(true);
299304
$table->method('checkIn')->willReturn(false);
300305
$table->method('getColumnAlias')->willReturn('checked_out');
306+
$table->method('getError')->willReturn('ERROR MESSAGE');
301307

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

317323
$this->assertFalse($model->checkout(1));
324+
325+
$this->expectException(\Exception::class);
326+
$model->setUseExceptions(true);
327+
$model->checkout(1);
318328
}
319329

320330
/**

tests/Unit/Libraries/Cms/Object/CMSObjectTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function testGetProperties()
102102
'_privateproperty1' => 'valuep1',
103103
'property1' => 'value1',
104104
'property2' => 5,
105+
'useExceptions' => false,
105106
],
106107
$object->getProperties(false),
107108
'Should get all properties, including private ones'

0 commit comments

Comments
 (0)