Reworked AJax action HTTP code and HTTP content. Added new HTTP template to process HTTP code output
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user