ObjectController: add explicit NotFoundError...

...handling for REST API requests

fixes #13641
This commit is contained in:
Thomas Gelf 2016-12-16 12:07:01 +01:00
parent 967baa330e
commit 251eb4f9a5

View File

@ -7,10 +7,8 @@ use Icinga\Exception\IcingaException;
use Icinga\Exception\InvalidPropertyException; use Icinga\Exception\InvalidPropertyException;
use Icinga\Exception\NotFoundError; use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Web\Url;
abstract class ObjectController extends ActionController abstract class ObjectController extends ActionController
{ {
@ -27,6 +25,23 @@ abstract class ObjectController extends ActionController
{ {
parent::init(); parent::init();
if ($this->getRequest()->isApiRequest()) {
$response = $this->getResponse();
try {
$this->loadObject();
return $this->handleApiRequest();
} catch (NotFoundError $e) {
$response->setHttpResponseCode(404);
return $this->sendJson((object) array('error' => $e->getMessage()));
} catch (Exception $e) {
if ($response->getHttpResponseCode() === 200) {
$response->setHttpResponseCode(500);
}
return $this->sendJson((object) array('error' => $e->getMessage()));
}
}
$type = strtolower($this->getType()); $type = strtolower($this->getType());
if ($object = $this->loadObject()) { if ($object = $this->loadObject()) {
@ -81,16 +96,7 @@ abstract class ObjectController extends ActionController
public function indexAction() public function indexAction()
{ {
if ($this->getRequest()->isApiRequest()) { if ($this->getRequest()->isApiRequest()) {
try { return;
return $this->handleApiRequest();
} catch (Exception $e) {
$response = $this->getResponse();
if ($response->getHttpResponseCode() === 200) {
$response->setHttpResponseCode(500);
}
return $this->sendJson((object) array('error' => $e->getMessage()));
}
} }
if ($this->object if ($this->object