diff --git a/lib/Controller/GenericApiControllerUtils.php b/lib/Controller/GenericApiControllerUtils.php index 47cf4160..ed9cbdbc 100644 --- a/lib/Controller/GenericApiControllerUtils.php +++ b/lib/Controller/GenericApiControllerUtils.php @@ -44,6 +44,20 @@ trait GenericApiControllerUtils return $user ? $user->getUID() : ''; } + /** + * Runa function and catch exceptions to return HTTP response. + */ + protected function guardEx($function): \OCP\AppFramework\Http\Response + { + try { + return $function(); + } catch (\OCA\Memories\HttpResponseException $e) { + return $e->response; + } catch (\Exception $e) { + return \OCA\Memories\Errors::Generic($e); + } + } + /** * Check if albums are enabled for this user. */ diff --git a/lib/Controller/GenericClusterController.php b/lib/Controller/GenericClusterController.php index 3dc639a0..85841a04 100644 --- a/lib/Controller/GenericClusterController.php +++ b/lib/Controller/GenericClusterController.php @@ -43,18 +43,12 @@ abstract class GenericClusterController extends GenericApiController */ public function list(): Http\Response { - try { + return $this->guardEx(function () { $this->init(); - // Get cluster list that will directly be returned as JSON $list = $this->getClusters(); - return new JSONResponse($list, Http::STATUS_OK); - } catch (HttpResponseException $e) { - return $e->response; - } catch (\Exception $e) { - return Errors::Generic($e); - } + }); } /** @@ -66,7 +60,7 @@ abstract class GenericClusterController extends GenericApiController */ public function preview(string $name): Http\Response { - try { + return $this->guardEx(function () use ($name) { $this->init(); // Get list of some photos in this cluster @@ -82,11 +76,7 @@ abstract class GenericClusterController extends GenericApiController // Get preview from image list return $this->getPreviewFromPhotoList($photos); - } catch (HttpResponseException $e) { - return $e->response; - } catch (\Exception $e) { - return Errors::Generic($e); - } + }); } /** @@ -98,7 +88,7 @@ abstract class GenericClusterController extends GenericApiController */ public function download(string $name): Http\Response { - try { + return $this->guardEx(function () use ($name) { $this->init(); // Get list of all files in this cluster @@ -110,11 +100,7 @@ abstract class GenericClusterController extends GenericApiController $handle = \OCA\Memories\Controller\DownloadController::createHandle($filename, $fileIds); return new JSONResponse(['handle' => $handle], Http::STATUS_OK); - } catch (HttpResponseException $e) { - return $e->response; - } catch (\Exception $e) { - return Errors::Generic($e); - } + }); } /**