From eefd1188195bfb8953706693dca2f6a7832d0abc Mon Sep 17 00:00:00 2001 From: Carsten Rose <carsten.rose@math.uzh.ch> Date: Fri, 27 Jan 2017 12:21:43 +0100 Subject: [PATCH] delete.php, QuickFormQuery.php: Error handling improved. --- extension/qfq/api/delete.php | 42 ++++++++++++++++++---------- extension/qfq/qfq/QuickFormQuery.php | 5 +++- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/extension/qfq/api/delete.php b/extension/qfq/api/delete.php index 373c1f653..0da62a021 100644 --- a/extension/qfq/api/delete.php +++ b/extension/qfq/api/delete.php @@ -13,6 +13,7 @@ use qfq; require_once(__DIR__ . '/../qfq/QuickFormQuery.php'); require_once(__DIR__ . '/../qfq/store/Store.php'); require_once(__DIR__ . '/../qfq/Constants.php'); +require_once(__DIR__ . '/../qfq/exceptions/CodeException.php'); /** @@ -47,14 +48,20 @@ require_once(__DIR__ . '/../qfq/Constants.php'); $answer = array(); $answer[API_REDIRECT] = API_ANSWER_REDIRECT_NO; -$answer[API_MESSAGE] = 'Something failed'; +$answer[API_MESSAGE] = ''; $answer[API_STATUS] = API_ANSWER_STATUS_ERROR; +$result[MSG_HEADER] = ''; +$result[MSG_CONTENT] = ''; + +$modeAnswer = false; +$flagSuccess = false; + try { $qfq = new \qfq\QuickFormQuery(['bodytext' => '']); - $qfq->delete(); + $flagSuccess = $qfq->delete(); $targetUrl = Store::getVar(SIP_TARGET_URL, STORE_SIP); $modeAnswer = Store::getVar(SIP_MODE_ANSWER, STORE_SIP); @@ -65,21 +72,14 @@ try { $answer[API_MESSAGE] = 'delete: success'; $answer[API_REDIRECT] = API_ANSWER_REDIRECT_CLIENT; $answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS; - - $result[MSG_HEADER] = "Content-Type: application/json"; - $result[MSG_CONTENT] = json_encode($answer); - break; case MODE_HTML: if ($targetUrl === false || $targetUrl === '') { - $result[MSG_CONTENT] = 'Missing target URL. ' . ERROR_MISSING_VALUE; - } else { - $result[MSG_CONTENT] = ''; + throw new CodeException('Missing target URL', ERROR_MISSING_VALUE); } $result[MSG_HEADER] = "Location: $targetUrl"; - break; default: @@ -100,8 +100,6 @@ try { $answer[API_FIELD_MESSAGE] = $val; } - $result[MSG_CONTENT] = json_encode($answer); - } catch (qfq\CodeException $e) { $answer[API_MESSAGE] = $e->formatMessage(); } catch (qfq\DbException $e) { @@ -110,13 +108,27 @@ try { $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage(); } -// Fallbak. in case an exception has been thrown and $result is not filled. -if (!isset($result[MSG_HEADER]) && !isset($result[MSG_CONTENT])) { +// In case $modeAnswer is still missing: try to get it again - maybe the SIP store has been initialized before the exception has been thrown. +if ($modeAnswer === false) { + $modeAnswer = Store::getVar(SIP_MODE_ANSWER, STORE_SIP); +} + +if ($modeAnswer === MODE_JSON) { + // JSON $result[MSG_HEADER] = "Content-Type: application/json"; $result[MSG_CONTENT] = json_encode($answer); +} else { + // HTML + if (!$flagSuccess) { + $result[MSG_CONTENT] = "<p>" . $answer[API_MESSAGE] . "</p>"; + if (isset($answer[API_FIELD_NAME])) { + $result[MSG_CONTENT] .= "<p>" . $answer[API_FIELD_NAME] . " : " . $answer[API_FIELD_MESSAGE] . "</p>"; + } + } } -if (isset($result[MSG_HEADER]) && $result[MSG_HEADER] !== '') { +// Sent header, if given. +if ($result[MSG_HEADER] !== '') { header($result[MSG_HEADER]); } diff --git a/extension/qfq/qfq/QuickFormQuery.php b/extension/qfq/qfq/QuickFormQuery.php index 02fca622e..5c4a99da7 100644 --- a/extension/qfq/qfq/QuickFormQuery.php +++ b/extension/qfq/qfq/QuickFormQuery.php @@ -236,7 +236,7 @@ class QuickFormQuery { // FORM_DELETE without a form definition: Fake the form wiht only a tableName. $table = $this->store->getVar(SIP_TABLE, STORE_SIP); if ($table === false) { - return ''; + throw new UserFormException("No 'form' and no 'table' definition found.", ERROR_MISSING_VALUE); } $sipFound = true; $this->formSpec[F_NAME] = ''; @@ -655,11 +655,14 @@ class QuickFormQuery { /** * Delete a record (tablename and recordid are given) or process a 'delete form' * + * @return bool * @throws CodeException */ public function delete() { $this->doForm(FORM_DELETE); + + return true; } /** -- GitLab