refactor: dedup try-catch
Signed-off-by: Varun Patil <varunpatil@ucla.edu>pull/563/head
parent
3f825073dc
commit
ca49eb19e8
|
@ -44,6 +44,20 @@ trait GenericApiControllerUtils
|
||||||
return $user ? $user->getUID() : '';
|
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.
|
* Check if albums are enabled for this user.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -43,18 +43,12 @@ abstract class GenericClusterController extends GenericApiController
|
||||||
*/
|
*/
|
||||||
public function list(): Http\Response
|
public function list(): Http\Response
|
||||||
{
|
{
|
||||||
try {
|
return $this->guardEx(function () {
|
||||||
$this->init();
|
$this->init();
|
||||||
|
|
||||||
// Get cluster list that will directly be returned as JSON
|
|
||||||
$list = $this->getClusters();
|
$list = $this->getClusters();
|
||||||
|
|
||||||
return new JSONResponse($list, Http::STATUS_OK);
|
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
|
public function preview(string $name): Http\Response
|
||||||
{
|
{
|
||||||
try {
|
return $this->guardEx(function () use ($name) {
|
||||||
$this->init();
|
$this->init();
|
||||||
|
|
||||||
// Get list of some photos in this cluster
|
// Get list of some photos in this cluster
|
||||||
|
@ -82,11 +76,7 @@ abstract class GenericClusterController extends GenericApiController
|
||||||
|
|
||||||
// Get preview from image list
|
// Get preview from image list
|
||||||
return $this->getPreviewFromPhotoList($photos);
|
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
|
public function download(string $name): Http\Response
|
||||||
{
|
{
|
||||||
try {
|
return $this->guardEx(function () use ($name) {
|
||||||
$this->init();
|
$this->init();
|
||||||
|
|
||||||
// Get list of all files in this cluster
|
// 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);
|
$handle = \OCA\Memories\Controller\DownloadController::createHandle($filename, $fileIds);
|
||||||
|
|
||||||
return new JSONResponse(['handle' => $handle], Http::STATUS_OK);
|
return new JSONResponse(['handle' => $handle], Http::STATUS_OK);
|
||||||
} catch (HttpResponseException $e) {
|
});
|
||||||
return $e->response;
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return Errors::Generic($e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue