From d4ace06ff54b8e3232e7d5730f7f159292924963 Mon Sep 17 00:00:00 2001 From: Paul Tirk Date: Wed, 24 Mar 2021 18:56:39 +0100 Subject: add json error response without an exception Signed-off-by: Paul Tirk --- lib/Controller/FolderApiV2Controller.php | 4 ++-- lib/Controller/JSONHttpErrorTrait.php | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'lib/Controller') diff --git a/lib/Controller/FolderApiV2Controller.php b/lib/Controller/FolderApiV2Controller.php index 6f2eb13da..a1fdcd44c 100644 --- a/lib/Controller/FolderApiV2Controller.php +++ b/lib/Controller/FolderApiV2Controller.php @@ -78,7 +78,7 @@ class FolderApiV2Controller extends ApiController try { $response = $this->folderService->rename($this->getUserId(), $folderId, $name); } catch (ServiceNotFoundException $ex) { - return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND); + return $this->errorResponseWithExceptionV2($ex, Http::STATUS_NOT_FOUND); } return $this->responseV2([ @@ -105,7 +105,7 @@ class FolderApiV2Controller extends ApiController 'folder' => $responseData ]); } catch (ServiceNotFoundException $ex) { - return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND); + return $this->errorResponseWithExceptionV2($ex, Http::STATUS_NOT_FOUND); } } } diff --git a/lib/Controller/JSONHttpErrorTrait.php b/lib/Controller/JSONHttpErrorTrait.php index 5c5d7a8cf..3defc47c1 100644 --- a/lib/Controller/JSONHttpErrorTrait.php +++ b/lib/Controller/JSONHttpErrorTrait.php @@ -30,13 +30,28 @@ trait JSONHttpErrorTrait * @param int $code * @return \OCP\AppFramework\Http\JSONResponse */ - public function errorResponseV2(\Exception $exception, $code) + public function errorResponseWithExceptionV2(\Exception $exception, $code) + { + return $this->errorResponseV2( + $exception->getMessage(), + $exception->getCode(), + $code + ); + } + + /** + * @param string $message + * @param int $code + * @param int $httpStatusCode + * @return \OCP\AppFramework\Http\JSONResponse + */ + public function errorResponseV2(string $message, int $code, int $httpStatusCode) { return new JSONResponse([ 'error' => [ - 'code' => $exception->getCode(), - 'message' => $exception->getMessage() + 'code' => $code, + 'message' => $message, ] - ], $code); + ], $httpStatusCode); } } -- cgit v1.2.3