icingaweb2/application/controllers/ErrorController.php
Eric Lippmann ce9110d22d Revert "Add proper respond http codes to service and host controller"
This reverts commit 6df031dc786256e0bd42f8047d6c308e90abedf6.

I revert this commit for the following reasons:
- MissingParameterException must not be thrown manually because we have UrlParams::getRequired() which was UrlParams::req() before.
- The commit introduces the untranslated string 'host or service'.
- 4xx are client, not server errors.
- Copy and paste code for the stack trace handling in the ErrorController.

refs #6281
2015-05-21 16:14:02 +02:00

73 lines
2.8 KiB
PHP

<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
use Icinga\Application\Icinga;
use Icinga\Application\Logger;
use Icinga\Exception\MissingParameterException;
use Icinga\Security\SecurityException;
use Icinga\Web\Controller\ActionController;
/**
* Application wide controller for displaying exceptions
*/
class ErrorController extends ActionController
{
protected $requiresAuthentication = false;
/**
* Display exception
*/
public function errorAction()
{
$error = $this->_getParam('error_handler');
$exception = $error->exception;
Logger::error($exception);
Logger::error('Stacktrace: %s', $exception->getTraceAsString());
switch ($error->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
$modules = Icinga::app()->getModuleManager();
$path = ltrim($this->_request->get('PATH_INFO'), '/');
$path = preg_split('~/~', $path);
$path = array_shift($path);
$this->getResponse()->setHttpResponseCode(404);
$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!'),
$path
);
}
break;
default:
switch (true) {
case $exception instanceof SecurityException:
$this->getResponse()->setHttpResponseCode(403);
break;
case $exception instanceof MissingParameterException:
$this->getResponse()->setHttpResponseCode(400);
$this->getResponse()->setHeader(
'X-Status-Reason',
'Missing parameter ' . $exception->getParameter()
);
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();
}
break;
}
$this->view->request = $error->request;
}
}