From 5cd544047379035cffb31ad8256999e45c67c8b9 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 12 Sep 2008 01:47:39 +0000 Subject: [PATCH] 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 --- core/control/HTTPResponse.php | 26 +++++++++++++++++++------- dev/Debug.php | 14 +++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/core/control/HTTPResponse.php b/core/control/HTTPResponse.php index 958446657..13bc04a33 100644 --- a/core/control/HTTPResponse.php +++ b/core/control/HTTPResponse.php @@ -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 { "; } 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"); } diff --git a/dev/Debug.php b/dev/Debug.php index a834a92ef..bf03e078a 100644 --- a/dev/Debug.php +++ b/dev/Debug.php @@ -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();