API CHANGE Changed parameters for Debug::friendlyError()

ENHANCEMENT Using DebugView class in Debug::friendlyError()
ENHANCEMENT Using Debug::friendlyError() in HTTPResponse class if status code signifies error (only on live environments in non-ajax requests)
ENHANCEMENT Showing contact information in Debug::friendlyError() from Email::getAdminEmail()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63465 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-10-01 14:43:43 +00:00
parent fd175cdbd3
commit 5d3e815967
4 changed files with 47 additions and 17 deletions

View File

@ -183,8 +183,13 @@ class HTTPResponse extends Object {
} }
} }
if(Director::isLive() && $this->isError()) {
Debug::friendlyError($this->statusCode, $this->getStatusDescription());
} else {
echo $this->body; echo $this->body;
} }
}
} }
/** /**

View File

@ -1,7 +1,9 @@
<?php <?php
/** /**
* ErrorPage holds the content for the page of an error response. * ErrorPage holds the content for the page of an error response.
*
* @package cms * @package cms
* @usedby Debug::friendlyError()
*/ */
class ErrorPage extends Page { class ErrorPage extends Page {

View File

@ -229,6 +229,7 @@ class Debug {
* @param unknown_type $errcontext * @param unknown_type $errcontext
*/ */
static function fatalHandler($errno, $errstr, $errfile, $errline, $errcontext) { static function fatalHandler($errno, $errstr, $errfile, $errline, $errcontext) {
if(self::$send_errors_to) self::emailError(self::$send_errors_to, $errno, $errstr, $errfile, $errline, $errcontext, "Error"); if(self::$send_errors_to) self::emailError(self::$send_errors_to, $errno, $errstr, $errfile, $errline, $errcontext, "Error");
self::log_error_if_necessary( $errno, $errstr, $errfile, $errline, $errcontext, "Error"); self::log_error_if_necessary( $errno, $errstr, $errfile, $errline, $errcontext, "Error");
@ -236,32 +237,47 @@ class Debug {
Debug::showError($errno, $errstr, $errfile, $errline, $errcontext, "Error"); Debug::showError($errno, $errstr, $errfile, $errline, $errcontext, "Error");
} else { } else {
Debug::friendlyError($errno, $errstr, $errfile, $errline, $errcontext); Debug::friendlyError();
} }
exit(1); exit(1);
} }
/** /**
* Render a user-facing error page, using the default HTML error template * Render a user-facing error page, using the default HTML error template
* if it exists. * rendered by {@link ErrorPage} if it exists. Doesn't use the standard {@link HTTPResponse} class
* the keep dependencies minimal.
* *
* @param unknown_type $errno * @uses ErrorPage
* @param unknown_type $errstr *
* @param unknown_type $errfile * @param int $statusCode HTTP Status Code (Default: 500)
* @param unknown_type $errline * @param string $friendlyErrorMessage User-focused error message. Should not contain code pointers or "tech-speak".
* @param unknown_type $errcontext * Used in the HTTP Header and ajax responses.
* @param string $friendlyErrorDetail Detailed user-focused message. Is just used if no {@link ErrorPage} is found
* for this specific status code.
* @return string HTML error message for non-ajax requests, plaintext for ajax-request.
*/ */
static function friendlyError($errno, $errstr, $errfile, $errline, $errcontext) { static function friendlyError($statusCode = 500, $friendlyErrorMessage = null, $friendlyErrorDetail = null) {
header("HTTP/1.0 500 There has been an error"); if(!$friendlyErrorMessage) $friendlyErrorMessage = 'There has been an error';
if(!$friendlyErrorDetail) $friendlyErrorDetail = 'The website server has not been able to respond to your request.';
if(!headers_sent()) header($_SERVER['SERVER_PROTOCOL'] . " $statusCode $friendlyErrorMessage");
if(Director::is_ajax()) { if(Director::is_ajax()) {
echo "There has been an error"; echo $friendlyErrorMessage;
} else {
if(file_exists(ASSETS_PATH . "/error-$statusCode.html")) {
echo file_get_contents(ASSETS_PATH . "/error-$statusCode.html");
} else {
$renderer = new DebugView();
$renderer->writeHeader();
$renderer->writeInfo("Website Error", $friendlyErrorMessage, $friendlyErrorDetail);
} else { if(Email::getAdminEmail()) {
if(file_exists(ASSETS_PATH . '/error-500.html')) { $mailto = Email::obfuscate(Email::getAdminEmail());
include(ASSETS_PATH . '/error-500.html'); $renderer->writeParagraph('Contact an administrator: ' . $mailto . '');
} else { }
echo "<h1>Error</h1><p>The website server has not been able to respond to your request.</p>\n";
$renderer->writeFooter();
} }
} }
} }
@ -288,7 +304,7 @@ class Debug {
if(!headers_sent()) { if(!headers_sent()) {
$errText = "$errtype: \"$errstr\" at line $errline of $errfile"; $errText = "$errtype: \"$errstr\" at line $errline of $errfile";
$errText = str_replace(array("\n","\r")," ",$errText); $errText = str_replace(array("\n","\r")," ",$errText);
header("HTTP/1.0 500 $errText"); if(!headers_sent()) header($_SERVER['SERVER_PROTOCOL'] . " 500 $errText");
// if error is displayed through ajax with CliDebugView, use plaintext output // if error is displayed through ajax with CliDebugView, use plaintext output
if(Director::is_ajax()) header('Content-Type: text/plain'); if(Director::is_ajax()) header('Content-Type: text/plain');
@ -598,7 +614,7 @@ class Debug {
$_SESSION['Security']['Message']['message'] = "You need to login with developer access to make use of debugging tools."; $_SESSION['Security']['Message']['message'] = "You need to login with developer access to make use of debugging tools.";
$_SESSION['Security']['Message']['type'] = 'warning'; $_SESSION['Security']['Message']['type'] = 'warning';
$_SESSION['BackURL'] = $_SERVER['REQUEST_URI']; $_SESSION['BackURL'] = $_SERVER['REQUEST_URI'];
header("HTTP/1.1 302 Found"); header($_SERVER['SERVER_PROTOCOL'] . " 302 Found");
header("Location: " . Director::baseURL() . "Security/login"); header("Location: " . Director::baseURL() . "Security/login");
die(); die();
} }

View File

@ -150,6 +150,13 @@ class DebugView {
Debug::backtrace(); Debug::backtrace();
echo '</div>'; echo '</div>';
} }
/**
* @param string $text
*/
function writeParagraph($text) {
echo '<p class="info">' . $text . '</p>';
}
} }
?> ?>