FEATURE: Update error handler to show useful information in the 500's status text

FEATURE: Allow customisation of HTTPResponse status text, as well as status code 


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@62286 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-09-12 01:47:39 +00:00
parent f2c619d10c
commit 5cd5440473
2 changed files with 28 additions and 12 deletions

View File

@ -57,6 +57,7 @@ class HTTPResponse extends Object {
);
protected $statusCode = 200;
protected $statusDescription = "OK";
/**
* HTTP Headers like "Content-Type: text/xml"
@ -71,9 +72,24 @@ class HTTPResponse extends Object {
*/
protected $body = null;
function setStatusCode($code) {
/**
* Create a new HTTP response
* @param $body The body of the response
* @param $statusCode The numeric status code - 200, 404, etc
* @param $statusDescription The text to be given alongside the status code. This can be accessed by javascript
*/
function __construct($body = null, $statusCode = null, $statusDescription = null) {
parent::__construct();
$this->body = $body;
if($statusCode) $this->setStatusCode($statusCode, $statusDescription);
}
function setStatusCode($code, $description = null) {
if(isset(self::$status_codes[$code])) $this->statusCode = $code;
else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING);
if($description) $this->statusDescription = $description;
else $this->statusDescription = self::$status_codes[$code];
}
function getStatusCode() {
@ -84,11 +100,7 @@ class HTTPResponse extends Object {
* @return string Description for a HTTP status code
*/
function getStatusDescription() {
if(isset(self::$status_codes[$this->statusCode])) {
return self::$status_codes[$this->statusCode];
} else {
return false;
}
return $this->statusDescription;
}
/**
@ -165,7 +177,7 @@ class HTTPResponse extends Object {
<script type=\"text/javascript\">setTimeout('window.location.href = \"$url\"', 50);</script>";
} else {
if(!headers_sent()) {
header("HTTP/1.1 $this->statusCode " . self::$status_codes[$this->statusCode]);
header("HTTP/1.1 $this->statusCode " . $this->getStatusDescription());
foreach($this->headers as $header => $value) {
header("$header: $value");
}

View File

@ -227,7 +227,7 @@ class Debug {
self::log_error_if_necessary( $errno, $errstr, $errfile, $errline, $errcontext, "Warning");
if(Director::isDev()) {
self::showError($errno, $errstr, $errfile, $errline, $errcontext);
self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Warning");
}
}
@ -247,7 +247,7 @@ class Debug {
self::log_error_if_necessary( $errno, $errstr, $errfile, $errline, $errcontext, "Error");
if(Director::isDev() || Director::is_cli()) {
Debug::showError($errno, $errstr, $errfile, $errline, $errcontext);
Debug::showError($errno, $errstr, $errfile, $errline, $errcontext, "Error");
} else {
Debug::friendlyError($errno, $errstr, $errfile, $errline, $errcontext);
@ -266,7 +266,7 @@ class Debug {
* @param unknown_type $errcontext
*/
static function friendlyError($errno, $errstr, $errfile, $errline, $errcontext) {
header("HTTP/1.0 500 Internal server error");
header("HTTP/1.0 500 There has been an error");
if(Director::is_ajax()) {
echo "There has been an error";
@ -298,8 +298,12 @@ class Debug {
* @param unknown_type $errline
* @param unknown_type $errcontext
*/
static function showError($errno, $errstr, $errfile, $errline, $errcontext) {
if(!headers_sent()) header("HTTP/1.0 500 Internal server error");
static function showError($errno, $errstr, $errfile, $errline, $errcontext, $errtype) {
if(!headers_sent()) {
$errText = "$errtype: \"$errstr\" at line $errline of $errfile";
$errText = str_replace(array("\n","\r")," ",$errText);
header("HTTP/1.0 500 $errText");
}
if(Director::is_ajax()) {
echo "ERROR:Error $errno: $errstr\n At l$errline in $errfile\n";
Debug::backtrace();