Skip to content

Commit

Permalink
[#496] Review error response codes in task operations.
Browse files Browse the repository at this point in the history
Web services return the code contained in the first OperationResult.
Codes have been reviewed to use 400 (bad request) and 403 (forbidden)
instead of 500 when they make more sense, which is most known error
cases.
  • Loading branch information
jaragunde committed Mar 8, 2023
1 parent ddbeb19 commit 82d4d97
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
11 changes: 7 additions & 4 deletions model/dao/TaskDAO/PostgreSQLTaskDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,14 @@ public function partialUpdate(DirtyTaskVO $taskVO) {
$resultMessage = "Task update failed:\n";
if(strpos($errorMessage, "end_after_init_task")) {
$resultMessage .= "Start time later than end time.";
$result->setResponseCode(400);
}
else {
$resultMessage .= $errorMessage;
$result->setResponseCode(500);
}
$result->setMessage($resultMessage);
$result->setIsSuccessful(false);
$result->setResponseCode(500);
}
else if (pg_affected_rows($res) == 1) {
$result->setIsSuccessful(true);
Expand Down Expand Up @@ -742,7 +743,7 @@ public function batchPartialUpdate($tasks) {
$result = new OperationResult(false);
$result->setErrorNumber(10);
$result->setMessage("Task update failed:\nDetected overlapping times.");
$result->setResponseCode(500);
$result->setResponseCode(400);
return array($result);
}

Expand Down Expand Up @@ -910,21 +911,23 @@ private function createInternal(TaskVO $taskVO) {
$resultMessage = "Error creating task:\n";
if(strpos($errorMessage, "end_after_init_task")) {
$resultMessage .= "Start time later than end time.";
$result->setResponseCode(400);
}
else if(strpos($errorMessage, "Not null violation")) {
$resultMessage .= "Missing compulsory data";
if(!$taskVO->getInit() || !$taskVO->getEnd()) {
$resultMessage .= ": init and/or end time";
}
$resultMessage .= ".";
$result->setResponseCode(400);
}
else {
$resultMessage .= $errorMessage;
$result->setResponseCode(500);
}
$result->setErrorNumber($ex->getCode());
$result->setMessage($resultMessage);
$result->setIsSuccessful(false);
$result->setResponseCode(500);
}

return $result;
Expand All @@ -942,7 +945,7 @@ public function batchCreate($tasks) {
$result = new OperationResult(false);
$result->setErrorNumber(10);
$result->setMessage("Task creation failed:\nDetected overlapping times.");
$result->setResponseCode(500);
$result->setResponseCode(400);
return array($result);
}

Expand Down
4 changes: 2 additions & 2 deletions model/facade/action/CreateTasksAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function doExecute() {
if (!$configDao->isWriteAllowedForDate($task->getDate())) {
$result = new OperationResult(false);
$result->setErrorNumber(20);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error creating task:\nNot allowed to write to date.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand All @@ -93,7 +93,7 @@ protected function doExecute() {
if (!$projectVO || !$projectVO->getActivation()) {
$result = new OperationResult(false);
$result->setErrorNumber(30);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error creating task:\nNot allowed to write to project.");
$discardedTasks[] = $task;
$discardedResults[] = $result;
Expand Down
6 changes: 3 additions & 3 deletions model/facade/action/DeleteReportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function doExecute() {
if(!$configDao->isWriteAllowedForDate($this->task->getDate())) {
$result = new OperationResult(false);
$result->setErrorNumber(20);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error deleting task:\nNot allowed to write to date.");
return $result;
}
Expand All @@ -87,7 +87,7 @@ protected function doExecute() {
if (!$dao->checkTaskUserId($this->task->getId(), $this->task->getUserId())) {
$result = new OperationResult(false);
$result->setErrorNumber(50);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error deleting task:\nBelongs to a different user.");
return $result;
}
Expand All @@ -99,7 +99,7 @@ protected function doExecute() {
if (!$projectVO || !$projectVO->getActivation()) {
$result = new OperationResult(false);
$result->setErrorNumber(30);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error updating task:\nNot allowed to write to project.");
return $result;
}
Expand Down
12 changes: 6 additions & 6 deletions model/facade/action/PartialUpdateTasksAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function doExecute() {
if(!$configDao->isWriteAllowedForDate($task->getDate())) {
$result = new OperationResult(false);
$result->setErrorNumber(20);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error updating task:\nNot allowed to write to date.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand All @@ -101,7 +101,7 @@ protected function doExecute() {
if (!isset($oldTask)) {
$result = new OperationResult(false);
$result->setErrorNumber(40);
$result->setResponseCode(500);
$result->setResponseCode(400);
$result->setMessage("Error updating task:\nTask does not exist.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand All @@ -113,7 +113,7 @@ protected function doExecute() {
if(!$configDao->isWriteAllowedForDate($oldTask->getDate())) {
$result = new OperationResult(false);
$result->setErrorNumber(20);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error updating task:\nNot allowed to write to date.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand All @@ -126,7 +126,7 @@ protected function doExecute() {
$task->getId(), $task->getUserId())) {
$result = new OperationResult(false);
$result->setErrorNumber(50);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error updating task:\nBelongs to a different user.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand All @@ -141,7 +141,7 @@ protected function doExecute() {
if (!$projectVO || !$projectVO->getActivation()) {
$result = new OperationResult(false);
$result->setErrorNumber(30);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error updating task:\nNot allowed to write to project.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand All @@ -156,7 +156,7 @@ protected function doExecute() {
if (!$projectVO || !$projectVO->getActivation()) {
$result = new OperationResult(false);
$result->setErrorNumber(30);
$result->setResponseCode(500);
$result->setResponseCode(403);
$result->setMessage("Error updating task:\nNot allowed to write to project.");
$discardedResults[] = $result;
$discardedTasks[] = $task;
Expand Down
4 changes: 2 additions & 2 deletions web/services/createTasksService.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@
return (!$item->getIsSuccessful());
});
if ($errors) {
//if multiple failures, let's just return a 500
http_response_code(500);
// if multiple failures, just return the code of the first one
http_response_code($errors[0]->getResponseCode());
$string = "<return service='createTasks'><errors>";
foreach((array) $errors as $result){
if (!$result->getIsSuccessful())
Expand Down
4 changes: 2 additions & 2 deletions web/services/deleteTasksService.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@
return (!$item->getIsSuccessful());
});
if ($errors) {
//if multiple failures, let's just return a 500
http_response_code(500);
// if multiple failures, just return the code of the first one
http_response_code($errors[0]->getResponseCode());
$string = "<return service='deleteTasks'><errors>";
foreach((array) $errors as $result){
if (!$result->getIsSuccessful())
Expand Down
4 changes: 2 additions & 2 deletions web/services/updateTasksService.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@
return (!$item->getIsSuccessful());
});
if ($errors) {
//if multiple failures, let's just return a 500
http_response_code(500);
// if multiple failures, just return the code of the first one
http_response_code($errors[0]->getResponseCode());
$string = "<return service='updateTasks'><errors>";
foreach((array) $errors as $result){
if (!$result->getIsSuccessful())
Expand Down

0 comments on commit 82d4d97

Please sign in to comment.