Skip to content

Commit

Permalink
Use exceptions instead of fatalError
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Jul 11, 2024
1 parent 280be75 commit ac0e09e
Show file tree
Hide file tree
Showing 36 changed files with 67 additions and 171 deletions.
2 changes: 1 addition & 1 deletion classes/cliTool/ScheduledTaskTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function executeTask($className, $args)
if (preg_match('/^[a-zA-Z0-9_.]+$/', $className)) {
// DEPRECATED as of 3.4.0: Use old class.name.style and import() function (pre-PSR classloading) pkp/pkp-lib#8186
if (!is_object($task = instantiate($className, null, null, 'execute', $args))) {
fatalError('Cannot instantiate task class.');
throw new \Exception('Cannot instantiate task class.');
}
} else {
$task = new $className($args);
Expand Down
2 changes: 1 addition & 1 deletion classes/components/forms/FormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function removeGroup($groupId): static
public function addPage($args, $position = []): static
{
if (empty($args['id'])) {
fatalError('Tried to add a form page without an id.');
throw new \Exception('Tried to add a form page without an id.');
}
if (empty($position)) {
$this->pages[] = $args;
Expand Down
8 changes: 4 additions & 4 deletions classes/controllers/grid/GridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,18 +829,18 @@ public function fetchCell(&$args, $request)
{
// Check the requested column
if (!isset($args['columnId'])) {
fatalError('Missing column id!');
throw new \Exception('Missing column id!');
}
if (!$this->hasColumn($args['columnId'])) {
fatalError('Invalid column id!');
throw new \Exception('Invalid column id!');
}
$this->setFirstDataColumn();
$column = $this->getColumn($args['columnId']);

// Instantiate the requested row
$row = $this->getRequestedRow($request, $args);
if (is_null($row)) {
fatalError('Row not found!');
throw new \Exception('Row not found!');
}

// Render the cell
Expand Down Expand Up @@ -924,7 +924,7 @@ protected function getRowInstance()
*/
protected function &getDataElementFromRequest($request, &$elementId)
{
fatalError('Grid does not support data element creation!');
throw new \Exception('Grid does not support data element creation!');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions classes/controllers/listbuilder/ListbuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function getNewRowId($request)
*/
public function deleteEntry($request, $rowId)
{
fatalError('ABSTRACT METHOD');
throw new \Exception('ABSTRACT METHOD');
}

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ public function updateEntry($request, $rowId, $newRowId)
*/
public function insertEntry($request, $newRowId)
{
fatalError('ABSTRACT METHOD');
throw new \Exception('ABSTRACT METHOD');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/file/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public function mkdirtree($dirPath, $perms = null)
if (!file_exists($dirPath)) {
//Avoid infinite recursion when file_exists reports false for root directory
if ($dirPath == dirname($dirPath)) {
fatalError('There are no readable files in this directory tree. Are safe mode or open_basedir active?');
throw new \Exception('There are no readable files in this directory tree. Are safe mode or open_basedir active?');
return false;
} elseif ($this->mkdirtree(dirname($dirPath), $perms)) {
return $this->mkdir($dirPath, $perms);
Expand Down
2 changes: 1 addition & 1 deletion classes/filter/PersistableFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function addSetting($setting)
// Check that the setting name does not
// collide with one of the internal settings.
if (in_array($settingName, $this->getInternalSettings())) {
fatalError('Trying to override an internal filter setting!');
throw new \Exception('Trying to override an internal filter setting!');
}

assert(!isset($this->_settings[$settingName]));
Expand Down
2 changes: 1 addition & 1 deletion classes/scheduledTask/ScheduledTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function addExecutionLogEntry($message, $type = null)
fwrite($fp, $log . PHP_EOL);
flock($fp, LOCK_UN);
} else {
fatalError("Couldn't lock the file.");
throw new \Exception("Couldn't lock the file.");
}
fclose($fp);
}
Expand Down
3 changes: 2 additions & 1 deletion classes/security/authorization/AllowedHostsPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function callOnDeny()
{
http_response_code(400);
error_log('Server host "' . $this->_request->getServerHost(null, false) . '" not allowed!');
fatalError('400 Bad Request');
echo "<h1>400 Server host not allowed</h1>\n";
exit;
}
}
2 changes: 1 addition & 1 deletion classes/site/VersionDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function insertVersion(Version $version, bool $isPlugin = false): int
$this->update('UPDATE versions SET current = 0 WHERE current = 1 AND product = ?', [$version->getProduct()]);
} else {
// We do not support downgrades.
fatalError('You are trying to downgrade the product "' . $version->getProduct() . '" from version [' . $oldVersion->getVersionString(false) . '] to version [' . $version->getVersionString(false) . ']. Downgrades are not supported.');
throw new \Exception('You are trying to downgrade the product "' . $version->getProduct() . '" from version [' . $oldVersion->getVersionString(false) . '] to version [' . $version->getVersionString(false) . ']. Downgrades are not supported.');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion classes/task/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected function moveFile(string $sourceDir, string $destDir, string $filename

// Script should always stop if it can't manipulate files inside
// its own directory system.
fatalError($message);
throw new \Exception($message);
}

return $destinationPath;
Expand Down
1 change: 0 additions & 1 deletion classes/template/PKPTemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ public function initialize(PKPRequest $request)
$this->registerPlugin('modifier', 'array_key_exists', array_key_exists(...));
$this->registerPlugin('modifier', 'array_key_first', array_key_first(...));
$this->registerPlugin('modifier', 'array_values', array_values(...));
$this->registerPlugin('modifier', 'fatalError', fatalError(...));
$this->registerPlugin('modifier', 'translate', __(...));
$this->registerPlugin('modifier', 'strip_unsafe_html', \PKP\core\PKPString::stripUnsafeHtml(...));
$this->registerPlugin('modifier', 'parse_url', parse_url(...));
Expand Down
2 changes: 1 addition & 1 deletion classes/xslt/XSLTransformationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($filterGroup, $displayName = 'XSL Transformation')
{
// Check that we only get xml input, the output type is arbitrary.
if (!substr($filterGroup->getInputType(), 0, 5) == 'xml::') {
fatalError('XSL filters need XML as input.');
throw new \Exception('XSL filters need XML as input.');
}

// Instantiate the settings of this filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($contextId, $fileId, $submissionId)
$this->libraryFile = $libraryFileDao->getById($fileId);

if (!$this->libraryFile || $this->libraryFile->getContextId() != $this->contextId || $this->libraryFile->getSubmissionId() != $this->getSubmissionId()) {
fatalError('Invalid library file!');
throw new \Exception('Invalid library file!');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ public function __construct($contextId, $fileId)
$this->libraryFile = $libraryFileDao->getById($fileId);

if (!$this->libraryFile || $this->libraryFile->getContextId() != $this->contextId) {
fatalError('Invalid library file!');
throw new \Exception('Invalid library file!');
}
}

/**
* Assign form data to user-submitted data.
*
* @see Form::readInputData()
*/
function readInputData() {
$this->readUserVars(array('temporaryFileId'));
public function readInputData()
{
$this->readUserVars(['temporaryFileId']);
return parent::readInputData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function updateReviewFormElement($args, $request)
$reviewForm = $reviewFormDao->getById($this->reviewFormId, Application::getContextAssocType(), $context->getId());

if (!$reviewFormDao->unusedReviewFormExists($this->reviewFormId, Application::getContextAssocType(), $context->getId()) || ($reviewFormElementId && !$reviewFormElementDao->reviewFormElementExists($reviewFormElementId, $this->reviewFormId))) {
fatalError('Invalid review form information!');
throw new \Exception('Invalid review form information!');
}

$reviewFormElementForm = new ReviewFormElementForm($this->reviewFormId, $reviewFormElementId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function execute(...$functionArgs)
$reviewFormDao = DAORegistry::getDAO('ReviewFormDAO'); /** @var ReviewFormDAO $reviewFormDao */
$reviewForm = $reviewFormDao->getById($reviewFormElement->getReviewFormId(), Application::getContextAssocType(), $context->getId());
if (!$reviewForm) {
fatalError('Invalid review form element ID!');
throw new \Exception('Invalid review form element ID!');
}
} else {
$reviewFormElement = $reviewFormElementDao->newDataObject();
Expand Down
2 changes: 1 addition & 1 deletion controllers/grid/settings/roles/UserGroupGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function authorize($request, &$args, $roleAssignments)
$userGroup = Repo::userGroup()->get($userGroupId);

if (!$userGroup) {
fatalError('Invalid user group id!');
throw new \Exception('Invalid user group id!');
} else {
$this->_userGroup = $userGroup;
}
Expand Down
6 changes: 4 additions & 2 deletions controllers/grid/settings/roles/form/UserGroupForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ public function execute(...$functionParams)
$userGroup = Repo::userGroup()->newDataObject();

$roleId = $this->getData('roleId');
if ($roleId == Role::ROLE_ID_SITE_ADMIN) throw new \Exception('Site administrator roles cannot be created here.');
if ($roleId == Role::ROLE_ID_SITE_ADMIN) {
throw new \Exception('Site administrator roles cannot be created here.');
}
$userGroup->setRoleId($roleId);

$userGroup->setContextId($this->getContextId());
Expand Down Expand Up @@ -291,7 +293,7 @@ public function _assignStagesToUserGroup($userGroupId, $userAssignedStages)
'stageId' => $stageId
]);
} else {
fatalError('Invalid stage id');
throw new \Exception('Invalid stage id');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/grid/users/author/form/PKPAuthorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function execute(...$functionParams)
} else {
$existingAuthor = true;
if ($publication->getId() !== $author->getData('publicationId')) {
fatalError('Invalid author!');
throw new \Exception('Invalid author!');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function execute(...$functionArgs)
$userGroupId = (int) $this->getData('userGroupId');

if (!$this->isValidUserAndGroup($userId, $userGroupId)) {
fatalError('invalid user or userGroup ID');
throw new \Exception('invalid user or userGroup ID');
}

Repo::userGroup()->assignUserToGroup($userId, $userGroupId);
Expand Down
2 changes: 1 addition & 1 deletion controllers/grid/users/reviewer/form/ReviewerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function execute(...$functionParams)
$reviewerId = (int) $this->getData('reviewerId');

if (!$this->_isValidReviewer($context, $submission, $currentReviewRound, $reviewerId)) {
fatalError('Invalid reviewer id.');
throw new \Exception('Invalid reviewer id.');
}

$reviewMethod = (int) $this->getData('reviewMethod');
Expand Down
2 changes: 1 addition & 1 deletion controllers/informationCenter/InformationCenterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function deleteNote($args, $request)
$note = $noteDao->getById($noteId);

if (!$request->checkCSRF() || !$note || $note->getAssocType() != $this->_getAssocType() || $note->getAssocId() != $this->_getAssocId()) {
fatalError('Invalid note!');
throw new \Exception('Invalid note!');
}
$noteDao->deleteById($noteId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function fetchReviewRoundInfo($args, $request)
$submission = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION);
$stageId = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_WORKFLOW_STAGE);
if ($stageId !== WORKFLOW_STAGE_ID_INTERNAL_REVIEW && $stageId !== WORKFLOW_STAGE_ID_EXTERNAL_REVIEW) {
fatalError('Invalid Stage Id');
throw new \Exception('Invalid Stage Id');
}

$templateMgr->assign([
Expand Down
18 changes: 5 additions & 13 deletions controllers/tab/workflow/PKPWorkflowTabHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,12 @@ private function _identifyStageId($request)

/**
* Identifies the review round.
*
* @param int $stageId
*
* @return string
*/
private function _identifyReviewRoundOp($stageId)
private function _identifyReviewRoundOp(int $stageId): string
{
switch ($stageId) {
case WORKFLOW_STAGE_ID_INTERNAL_REVIEW:
return 'internalReviewRound';
case WORKFLOW_STAGE_ID_EXTERNAL_REVIEW:
return 'externalReviewRound';
default:
fatalError('unknown review round id.');
}
return match($stageId) {
WORKFLOW_STAGE_ID_INTERNAL_REVIEW => 'internalReviewRound',
WORKFLOW_STAGE_ID_EXTERNAL_REVIEW => 'externalReviewRound',
};
}
}
10 changes: 5 additions & 5 deletions controllers/wizard/fileUpload/FileUploadWizardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function authorize($request, &$args, $roleAssignments)
if ($submissionFileIdToValidate) {
$this->addPolicy(new SubmissionFileAccessPolicy($request, $args, $roleAssignments, SubmissionFileAccessPolicy::SUBMISSION_FILE_ACCESS_MODIFY, $submissionFileIdToValidate));

// Allow uploading to review attachments
// Allow uploading to review attachments
} elseif ($fileStage === SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT) {
$assocType = (int) $request->getUserVar('assocType');
$assocId = (int) $request->getUserVar('assocId');
Expand All @@ -137,7 +137,7 @@ public function authorize($request, &$args, $roleAssignments)
$this->addPolicy(new ReviewRoundRequiredPolicy($request, $args));
$this->addPolicy(new ReviewAssignmentFileWritePolicy($request, $assocId));

// Allow uploading to a note
// Allow uploading to a note
} elseif ($fileStage === SubmissionFile::SUBMISSION_FILE_QUERY) {
$assocType = (int) $request->getUserVar('assocType');
$assocId = (int) $request->getUserVar('assocId');
Expand All @@ -149,7 +149,7 @@ public function authorize($request, &$args, $roleAssignments)
$this->addPolicy(new QueryAccessPolicy($request, $args, $roleAssignments, $stageId));
$this->addPolicy(new NoteAccessPolicy($request, $assocId, NoteAccessPolicy::NOTE_ACCESS_WRITE));

// Allow uploading a dependent file to another file
// Allow uploading a dependent file to another file
} elseif ($fileStage === SubmissionFile::SUBMISSION_FILE_DEPENDENT) {
$assocType = (int) $request->getUserVar('assocType');
$assocId = (int) $request->getUserVar('assocId');
Expand All @@ -159,7 +159,7 @@ public function authorize($request, &$args, $roleAssignments)

$this->addPolicy(new SubmissionFileAccessPolicy($request, $args, $roleAssignments, SubmissionFileAccessPolicy::SUBMISSION_FILE_ACCESS_MODIFY, $assocId));

// Allow uploading to other file stages in the workflow
// Allow uploading to other file stages in the workflow
} else {
$stageId = (int) $request->getUserVar('stageId');
$assocType = (int) $request->getUserVar('assocType');
Expand Down Expand Up @@ -207,7 +207,7 @@ public function initialize($request)
$uploaderRoles = explode('-', $uploaderRoles);
foreach ($uploaderRoles as $uploaderRole) {
if (!is_numeric($uploaderRole)) {
fatalError('Invalid uploader role!');
throw new \Exception('Invalid uploader role!');
}
$this->_uploaderRoles[] = (int)$uploaderRole;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function __construct(
!is_numeric($fileStage) || $fileStage <= 0 ||
!is_numeric($stageId) || $stageId < 1 || $stageId > 5 ||
isset($assocType) !== isset($assocId)) {
fatalError('Invalid parameters!');
throw new \Exception('Invalid parameters!');
}

// Initialize class.
Expand All @@ -89,7 +89,7 @@ public function __construct(
// Get the review assignment object.
$reviewAssignment = Repo::reviewAssignment()->get((int) $assocId);
if ($reviewAssignment->getDateCompleted()) {
fatalError('Review already completed!');
throw new \Exception('Review already completed!');
}

// Get the review round object.
Expand Down Expand Up @@ -261,7 +261,7 @@ public function getRevisionSubmissionFilesSelection($user, $uploadedFile = null)
->withStageIds([$this->getStageId()])
->withUserId($user->getId())
->exists();

if (
($submissionFile->getFileStage() == SubmissionFile::SUBMISSION_FILE_REVIEW_ATTACHMENT || $submissionFile->getFileStage() == SubmissionFile::SUBMISSION_FILE_REVIEW_FILE) &&
$hasAnyAssignments
Expand Down Expand Up @@ -351,7 +351,7 @@ public function fetch($request, $template = null, $display = false)
// Make sure that the revised file (if any) really was among
// the retrieved submission files in the current file stage.
if ($revisedFileId && !$foundRevisedFile) {
fatalError('Invalid revised file id!');
throw new \Exception('Invalid revised file id!');
}

// Set the review file candidate data in the template.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function __construct(
assert(is_null($uploaderRoles) || (is_array($uploaderRoles) && count($uploaderRoles) >= 1));
$this->_uploaderRoles = $uploaderRoles;

if (!$revisionOnly && empty($submissionFileOptions) && is_numeric($revisedFileId)) {
throw new \Exception('A revised file id cannot be given when uploading a new file!');
}

parent::__construct(
$request,
'controllers/wizard/fileUpload/form/fileUploadForm.tpl',
Expand Down
Loading

0 comments on commit ac0e09e

Please sign in to comment.