Finished Error handling across the API

Signed-off-by: Ryan Fletcher <ryan.fletcher@codepassion.ca>
This commit is contained in:
Ryan Fletcher
2018-07-13 19:07:08 -04:00
committed by Julius Härtl
parent 2668f6b80c
commit dd1d4246fe
5 changed files with 34 additions and 44 deletions

View File

@@ -68,7 +68,7 @@ class CardApiController extends ApiController {
* Get a specific card. * Get a specific card.
*/ */
public function get() { public function get() {
$boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], $this->boardService); $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
} }
@@ -104,7 +104,7 @@ class CardApiController extends ApiController {
*/ */
public function create($title, $type = 'plain', $order = 999) { public function create($title, $type = 'plain', $order = 999) {
$boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
} }
@@ -148,7 +148,7 @@ class CardApiController extends ApiController {
*/ */
public function update($title, $type, $order, $description = null, $duedate = null, $archive = false, $assignedUserId = 0) { public function update($title, $type, $order, $description = null, $duedate = null, $archive = false, $assignedUserId = 0) {
$boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
} }
@@ -217,7 +217,7 @@ class CardApiController extends ApiController {
*/ */
public function delete() { public function delete() {
$boardError = $this->apiHelper->boardHasError($this->request->params['boardId'], 'board', $this->boardService); $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
} }

View File

@@ -28,35 +28,17 @@ use OCP\AppFramework\Http;
class ApiHelper { class ApiHelper {
public static function boardHasError($boardId, $boardService) { public static function entityHasError($entityId, $entityName, $service) {
if (is_numeric($boardId) === false) { if (is_numeric($entityId) === false) {
$error['message'] = 'board id must be a number'; $error['message'] = $entityName . ' id must be a number';
$error['status'] = HTTP::STATUS_BAD_REQUEST; $error['status'] = HTTP::STATUS_BAD_REQUEST;
return $error; return $error;
} }
$board = $boardService->find($boardId); $entity = $service->find($entityId);
if ($board === false || $board === null) { if ($entity === false || $entity === null) {
$error['message'] = 'board does not exist'; $error['message'] = $entityName . ' does not exist';
$error['status'] = HTTP::STATUS_NOT_FOUND;
return $error;
}
return false;
}
public static function stackHasError($stackId, $stackService) {
if (is_numeric($stackId) === false) {
$error['message'] = 'board id must be a number';
$error['status'] = HTTP::STATUS_BAD_REQUEST;
return $error;
}
$stack = $stackService->find($stackId);
if ($stack === false || $stack === null) {
$error['message'] = 'stack does not exist';
$error['status'] = HTTP::STATUS_NOT_FOUND; $error['status'] = HTTP::STATUS_NOT_FOUND;
return $error; return $error;
} }

View File

@@ -27,8 +27,9 @@ use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest; use OCP\IRequest;
use OCA\Deck\Service\LabelService; use OCA\Deck\Service\LabelService;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Controller\Helper\ApiHelper;
/** /**
* Class BoardApiController * Class BoardApiController
@@ -46,10 +47,12 @@ class LabelApiController extends ApiController {
* @param LabelService $service * @param LabelService $service
* @param $userId * @param $userId
*/ */
public function __construct($appName, IRequest $request, LabelService $labelService, $userId) { public function __construct($appName, IRequest $request, LabelService $labelService, BoardService $boardService, $userId) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->labelService = $labelService; $this->labelService = $labelService;
$this->boardService = $boardService;
$this->userId = $userId; $this->userId = $userId;
$this->apiHelper = new ApiHelper();
} }
/** /**
@@ -61,8 +64,9 @@ class LabelApiController extends ApiController {
*/ */
public function get() { public function get() {
if (is_numeric($this->request->params['boardId']) === false) { $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
} }
if (is_numeric($this->request->params['labelId']) === false) { if (is_numeric($this->request->params['labelId']) === false) {
@@ -89,8 +93,9 @@ class LabelApiController extends ApiController {
*/ */
public function create($title, $color) { public function create($title, $color) {
if (is_numeric($this->request->params['boardId']) === false) { $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
} }
if ($title === false || $title === null) { if ($title === false || $title === null) {
@@ -121,8 +126,9 @@ class LabelApiController extends ApiController {
*/ */
public function update($title, $color) { public function update($title, $color) {
if (is_numeric($this->request->params['boardId']) === false) { $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
} }
if (is_numeric($this->request->params['labelId']) === false) { if (is_numeric($this->request->params['labelId']) === false) {
@@ -155,8 +161,9 @@ class LabelApiController extends ApiController {
*/ */
public function delete() { public function delete() {
if (is_numeric($this->request->params['boardId']) === false) { $boardError = $this->apiHelper->entityHasError($this->request->params['boardId'], 'board', $this->boardService);
return new DataResponse('board id must be a number', HTTP::STATUS_BAD_REQUEST); if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']);
} }
if (is_numeric($this->request->params['labelId']) === false) { if (is_numeric($this->request->params['labelId']) === false) {

View File

@@ -66,7 +66,7 @@ class StackApiController extends ApiController {
* Return all of the stacks in the specified board. * Return all of the stacks in the specified board.
*/ */
public function index() { public function index() {
$boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService );
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
@@ -95,7 +95,7 @@ class StackApiController extends ApiController {
*/ */
public function create($title, $order) { public function create($title, $order) {
$boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService );
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
@@ -127,7 +127,7 @@ class StackApiController extends ApiController {
*/ */
public function update($title, $order) { public function update($title, $order) {
$boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService );
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);
@@ -163,7 +163,7 @@ class StackApiController extends ApiController {
*/ */
public function delete() { public function delete() {
$boardError = $this->apiHelper->boardHasError( $this->request->params['boardId'], $this->boardService ); $boardError = $this->apiHelper->entityHasError( $this->request->params['boardId'], 'board', $this->boardService );
if ($boardError) { if ($boardError) {
return new DataResponse($boardError['message'], $boardError['status']); return new DataResponse($boardError['message'], $boardError['status']);

View File

@@ -89,6 +89,7 @@ class StackService {
} }
} }
//TODO: Write this function so we can look up one stack id.
public function find($stackId) { public function find($stackId) {
throw new \Exception('Not yet implemented'); throw new \Exception('Not yet implemented');
} }