示例#1
0
/**
 * The PieCrust shutdown function.
 */
function piecrust_shutdown_function()
{
    $error = error_get_last();
    if ($error) {
        try {
            $guard = 100;
            while (ob_get_level() > 0 && --$guard > 0) {
                ob_end_clean();
            }
        } catch (Exception $e) {
        }
        piecrust_show_system_message('critical', "{$error['message']} in '{$error['file']}:{$error['line']}'");
        exit;
    }
}
 /**
  * Handles an exception by showing an appropriate
  * error page.
  */
 public function handleError(Exception $e, $server = null, array &$headers = null)
 {
     $displayErrors = $this->pieCrust->isDebuggingEnabled() || $this->pieCrust->getConfig()->getValue('site/display_errors');
     // If debugging is enabled, just display the error and exit.
     if ($displayErrors) {
         if ($e->getMessage() == '404') {
             //TODO: set header?
             piecrust_show_system_message('404');
             return;
         }
         $errorMessage = self::formatException($e, true);
         piecrust_show_system_message('error', $errorMessage);
         return;
     }
     // First of all, check that we're not running
     // some completely brand new and un-configured website.
     if ($this->isEmptySetup()) {
         piecrust_show_system_message('welcome');
         return;
     }
     // Generic error message in case we don't have anything custom.
     $errorMessage = "<p>We're sorry but something very wrong happened, and we don't know what. " . "We'll try to do better next time.</p>" . PHP_EOL;
     // Get the URI to the custom error page, and the error code.
     if ($e->getMessage() == '404') {
         HttpHeaderHelper::setOrAddHeader(0, 404, $headers);
         $errorPageUri = '_404';
     } else {
         HttpHeaderHelper::setOrAddHeader(0, 500, $headers);
         $errorPageUri = '_error';
     }
     // Get the error page's info.
     try {
         $errorPageUriInfo = UriParser::parseUri($this->pieCrust, $errorPageUri, UriParser::PAGE_URI_REGULAR);
     } catch (Exception $inner) {
         // What the f**k.
         piecrust_show_system_message('critical', $errorMessage);
         return;
     }
     // Render the error page (either a custom one, or a generic one).
     if ($errorPageUriInfo != null and is_file($errorPageUriInfo['path'])) {
         // We have a custom error page. Show it, or display
         // the "fatal error" page if even this doesn't work.
         try {
             $runner = new PieCrustRunner($this->pieCrust);
             $runner->runUnsafe($errorPageUri, $server, null, $headers);
         } catch (Exception $inner) {
             // Well there's really something wrong.
             piecrust_show_system_message('critical', $errorMessage);
         }
     } else {
         // We don't have a custom error page. Just show the generic
         // error page.
         $errorMessage = self::formatException($e, false);
         piecrust_show_system_message(substr($errorPageUri, 1), $errorMessage);
     }
 }