0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-11-23 05:00:27 +01:00
organization_folders/lib/Controller/Errors.php

35 lines
937 B
PHP
Raw Normal View History

<?php
namespace OCA\OrganizationFolders\Controller;
use Closure;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCA\OrganizationFolders\Errors\NotFoundException;
trait Errors {
private function errorResponse(\Exception $e, $status = Http::STATUS_BAD_REQUEST): JSONResponse {
$response = ['error' => get_class($e), 'message' => $e->getMessage()];
return new JSONResponse($response, $status);
}
protected function handleNotFound(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback());
} catch (NotFoundException $e) {
return $this->errorResponse($e, Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
return $this->errorResponse($e);
}
}
protected function handleErrors(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback());
} catch (\Exception $e) {
return $this->errorResponse($e);
}
}
}