diff --git a/application/controllers/ErrorController.php b/application/controllers/ErrorController.php index b1343dc81..0223a66b7 100644 --- a/application/controllers/ErrorController.php +++ b/application/controllers/ErrorController.php @@ -3,6 +3,8 @@ use Icinga\Application\Icinga; use Icinga\Application\Logger; +use Icinga\Exception\Http\HttpMethodNotAllowedException; +use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Exception\MissingParameterException; use Icinga\Security\SecurityException; use Icinga\Web\Controller\ActionController; @@ -34,11 +36,7 @@ class ErrorController extends ActionController $path = preg_split('~/~', $path); $path = array_shift($path); $this->getResponse()->setHttpResponseCode(404); - $title = preg_replace('/\r?\n.*$/s', '', $exception->getMessage()); - $this->view->title = 'Server error: ' . $title; - if ($this->getInvokeArg('displayExceptions')) { - $this->view->stackTrace = $exception->getTraceAsString(); - } + $this->view->message = $this->translate('Page not found.'); if ($modules->hasInstalled($path) && ! $modules->hasEnabled($path)) { $this->view->message .= ' ' . sprintf( $this->translate('Enabling the "%s" module might help!'), @@ -49,8 +47,12 @@ class ErrorController extends ActionController break; default: switch (true) { - case $exception instanceof SecurityException: - $this->getResponse()->setHttpResponseCode(403); + case $exception instanceof HttpMethodNotAllowedException: + $this->getResponse()->setHttpResponseCode(405); + $this->getResponse()->setHeader('Allow', $exception->getAllowedMethods()); + break; + case $exception instanceof HttpNotFoundException: + $this->getResponse()->setHttpResponseCode(404); break; case $exception instanceof MissingParameterException: $this->getResponse()->setHttpResponseCode(400); @@ -59,12 +61,13 @@ class ErrorController extends ActionController 'Missing parameter ' . $exception->getParameter() ); break; + case $exception instanceof SecurityException: + $this->getResponse()->setHttpResponseCode(403); + break; default: $this->getResponse()->setHttpResponseCode(500); break; } - $title = preg_replace('/\r?\n.*$/s', '', $exception->getMessage()); - $this->view->title = 'Server error: ' . $title; $this->view->message = $exception->getMessage(); if ($this->getInvokeArg('displayExceptions')) { $this->view->stackTrace = $exception->getTraceAsString(); diff --git a/application/views/scripts/error/error.phtml b/application/views/scripts/error/error.phtml index d9422474c..5b3480922 100644 --- a/application/views/scripts/error/error.phtml +++ b/application/views/scripts/error/error.phtml @@ -1,13 +1,8 @@
tabs->showOnlyCloseButton() ?> -title): ?> -

escape($title) ?>

-
-message): ?>

escape($message)) ?>

-
escape($stackTrace) ?>
diff --git a/library/Icinga/Exception/Http/HttpException.php b/library/Icinga/Exception/Http/HttpException.php new file mode 100644 index 000000000..4f47f5b67 --- /dev/null +++ b/library/Icinga/Exception/Http/HttpException.php @@ -0,0 +1,13 @@ +allowedMethods; + } + + /** + * Set the allowed HTTP methods + * + * @param string $allowedMethods + * + * @return $this + */ + public function setAllowedMethods($allowedMethods) + { + $this->allowedMethods = (string) $allowedMethods; + return $this; + } +} diff --git a/library/Icinga/Exception/Http/HttpNotFoundException.php b/library/Icinga/Exception/Http/HttpNotFoundException.php new file mode 100644 index 000000000..8ec6b7fcb --- /dev/null +++ b/library/Icinga/Exception/Http/HttpNotFoundException.php @@ -0,0 +1,11 @@ +getRequest()->getMethod()])) { - $this->getResponse()->setHeader('Allow', implode(', ', array_keys($httpMethods))); - throw new \Zend_Controller_Action_Exception($this->translate('Method Not Allowed'), 405); + $e = new HttpMethodNotAllowedException($this->translate('Method Not Allowed')); + $e->setAllowedMethods(implode(', ', array_keys($httpMethods))); + throw $e; } } diff --git a/modules/monitoring/application/controllers/CommentController.php b/modules/monitoring/application/controllers/CommentController.php index c3fcd18f8..4237d598b 100644 --- a/modules/monitoring/application/controllers/CommentController.php +++ b/modules/monitoring/application/controllers/CommentController.php @@ -20,13 +20,11 @@ class Monitoring_CommentController extends Controller /** * Fetch the first comment with the given id and add tabs - * - * @throws Zend_Controller_Action_Exception */ public function init() { - $commentId = $this->params->get('comment_id'); - + $commentId = $this->params->getRequired('comment_id'); + $this->comment = $this->backend->select()->from('comment', array( 'id' => 'comment_internal_id', 'objecttype' => 'comment_objecttype', @@ -41,9 +39,9 @@ class Monitoring_CommentController extends Controller 'host_display_name', 'service_display_name' ))->where('comment_internal_id', $commentId)->getQuery()->fetchRow(); - - if (false === $this->comment) { - throw new Zend_Controller_Action_Exception($this->translate('Comment not found')); + + if ($this->comment === false) { + $this->httpNotFound($this->translate('Comment not found')); } $this->getTabs()->add( @@ -88,7 +86,7 @@ class Monitoring_CommentController extends Controller private function createDelCommentForm() { $this->assertPermission('monitoring/command/comment/delete'); - + $delCommentForm = new DeleteCommentCommandForm(); $delCommentForm->setAction( Url::fromPath('monitoring/comment/show') diff --git a/modules/monitoring/application/controllers/DowntimeController.php b/modules/monitoring/application/controllers/DowntimeController.php index c06e0311d..67b8b6a33 100644 --- a/modules/monitoring/application/controllers/DowntimeController.php +++ b/modules/monitoring/application/controllers/DowntimeController.php @@ -30,13 +30,11 @@ class Monitoring_DowntimeController extends Controller /** * Fetch the downtime matching the given id and add tabs - * - * @throws Zend_Controller_Action_Exception */ public function init() { - $downtimeId = $this->params->get('downtime_id'); - + $downtimeId = $this->params->getRequired('downtime_id'); + $this->downtime = $this->backend->select()->from('downtime', array( 'id' => 'downtime_internal_id', 'objecttype' => 'downtime_objecttype', @@ -60,17 +58,17 @@ class Monitoring_DowntimeController extends Controller 'host_display_name', 'service_display_name' ))->where('downtime_internal_id', $downtimeId)->getQuery()->fetchRow(); - - if (false === $this->downtime) { - throw new Zend_Controller_Action_Exception($this->translate('Downtime not found')); + + if ($this->downtime === false) { + $this->httpNotFound($this->translate('Downtime not found')); } - + if (isset($this->downtime->service_description)) { $this->isService = true; } else { $this->isService = false; } - + $this->getTabs() ->add( 'downtime', diff --git a/modules/monitoring/application/controllers/HostController.php b/modules/monitoring/application/controllers/HostController.php index 7693023eb..464da200c 100644 --- a/modules/monitoring/application/controllers/HostController.php +++ b/modules/monitoring/application/controllers/HostController.php @@ -1,7 +1,6 @@ params->get('host') === null) { - throw new MissingParameterException( - $this->translate('Required parameter \'%s\' is missing'), - 'host' - ); - } - - $host = new Host($this->backend, $this->params->get('host')); + $host = new Host($this->backend, $this->params->getRequired('host')); $this->applyRestriction('monitoring/hosts/filter', $host); if ($host->fetch() === false) { - throw new Zend_Controller_Action_Exception( - sprintf($this->translate('Host \'%s\' not found'), $this->params->get('host')), - 404 - ); + $this->httpNotFound($this->translate('Host not found')); } $this->object = $host; $this->createTabs(); diff --git a/modules/monitoring/application/controllers/ServiceController.php b/modules/monitoring/application/controllers/ServiceController.php index e61c4685d..2886650c9 100644 --- a/modules/monitoring/application/controllers/ServiceController.php +++ b/modules/monitoring/application/controllers/ServiceController.php @@ -1,7 +1,6 @@ params->get('host') === null || $this->params->get('service') === null) { - throw new MissingParameterException( - $this->translate('One of the required parameters \'%s\' is missing'), - 'host or service' - ); - } - - $service = new Service($this->backend, $this->params->get('host'), $this->params->get('service')); + $service = new Service( + $this->backend, $this->params->getRequired('host'), $this->params->getRequired('service') + ); $this->applyRestriction('monitoring/services/filter', $service); if ($service->fetch() === false) { - throw new Zend_Controller_Action_Exception( - sprintf($this->translate('Service \'%s\' not found'), $this->params->get('service')), - 404 - ); + $this->httpNotFound($this->translate('Service not found')); } $this->object = $service; $this->createTabs();