Reworked AJax action HTTP code and HTTP content. Added new HTTP template to process HTTP code output

This commit is contained in:
Anton Terekhov
2012-11-11 20:42:48 +04:00
parent 618f12224e
commit e1c3da8019
4 changed files with 106 additions and 39 deletions

View File

@ -5,18 +5,18 @@
* @package Majestic
* @subpackage app
* @since 2010-02-25
* @version SVN: $Id$
* @filesource $URL$
*/
class ErrorAction extends Action
{
/**
* @var ErrorException
* @var ErrorException|ErrorHTTPException
*/
public $exception;
protected $ajax_error = false;
public function __construct($exception)
{
$this->exception = $exception;
@ -27,18 +27,22 @@ class ErrorAction extends Action
{
$this->template = 500;
if ($this->exception instanceof Error404Exception) {
if ($this->isAjaxActionError()) {
if (!headers_sent()) {
header('HTTP/1.0 404 Not Found');
die();
}
}
$this->template = 404;
} elseif ($this->exception instanceof ErrorHTTPException) {
$this->template = 'HTTP';
}
$this->logError();
$this->sendHTTPCode();
}
public function fetch()
{
if ($this->isAjaxActionError()) {
return $this->exception->getMessage();
}
return parent::fetch();
}
protected function getTemplate()
{
return '/actions/' . $this->template;
@ -46,22 +50,19 @@ class ErrorAction extends Action
protected function sendHttpCode()
{
if (headers_sent()) {
return;
}
switch ($this->template) {
case 404:
header('HTTP/1.0 404 Not Found');
break;
case 'HTTP':
header($this->exception->getHTTPHeader());
break;
default:
header('HTTP/1.0 500 Internal Server Error');
}
}
protected function logError()
{
if ($this->template == 500) {
$error = 0;
$ex = $this->exception;
if ($ex instanceof ErrorException) {
@ -83,7 +84,7 @@ class ErrorAction extends Action
break;
}
$message = 'PHP ' . $error . ': ' . $ex->getMessage() . ' in ' . $ex->getFile()
. ' on line ' . $ex->getLine();
. ' on line ' . $ex->getLine();
error_log($message);
}
}
@ -94,12 +95,14 @@ class ErrorAction extends Action
*/
protected function isAjaxActionError()
{
$trace = $this->exception->getTrace();
foreach ($trace as $line) {
if (isset($line['class']) && $line['class'] === 'AjaxAction') {
return true;
}
}
return false;
return $this->ajax_error;
}
/**
* Set if exception was thrown from AjaxAction subclass
*/
public function setAjaxError()
{
$this->ajax_error = true;
}
}

View File

@ -142,7 +142,11 @@ class FrontController
*/
$layout = new $layout_class();
$layout->setException($e);
return $layout->fetch(new ErrorAction($e));
$error_action = new ErrorAction($e);
if (isset($action) && is_subclass_of($action, 'AjaxAction')) {
$error_action->setAjax();
}
return $layout->fetch($error_action);
}
}
}