Added ability to enable/disable error display and logging

This commit is contained in:
Andy Miller
2014-10-30 16:19:05 -06:00
parent ff1db8ece8
commit 051a7e66f6
8 changed files with 244 additions and 17 deletions

View File

@@ -40,6 +40,10 @@ assets: # Configuration for Assets Manager (JS, C
js_pipeline: false # The JS pipeline is the unification of multiple JS resources into one file
js_minify: true # Minify the JS during pipelining
errors:
display: true # Display full backtrace-style error page
log: true # Log errors to /logs folder
debugger:
enabled: false # Enable Grav debugger and following settings
twig: true # Enable debugging of Twig templates

View File

@@ -0,0 +1,57 @@
<?php
namespace Grav\Common\Errors;
use Grav\Common\Grav;
use Whoops\Handler\CallbackHandler;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Handler\PlainTextHandler;
use Whoops\Run;
/**
* Class Debugger
* @package Grav\Common
*/
class Errors extends \Whoops\Run
{
public function pushHandler($handler, $key = null)
{
if (is_callable($handler)) {
$handler = new CallbackHandler($handler);
}
if (!$handler instanceof HandlerInterface) {
throw new InvalidArgumentException(
"Argument to " . __METHOD__ . " must be a callable, or instance of"
. "Whoops\\Handler\\HandlerInterface"
);
}
// Store with key if provided
if ($key) {
$this->handlerStack[$key] = $handler;
} else {
$this->handlerStack[] = $handler;
}
return $this;
}
public function resetHandlers()
{
$grav = Grav::instance();
$config = $grav['config']->get('system.errors');
if (isset($config['display']) && !$config['display']) {
unset($this->handlerStack['pretty']);
unset($this->handlerStack['text']);
unset($this->handlerStack['json']);
$this->handlerStack = array('simple' => new SimplePageHandler()) + $this->handlerStack;
}
if (isset($config['log']) && !$config['log']) {
unset($this->handlerStack['log']);
}
}
}

View File

@@ -0,0 +1,50 @@
html, body {
height: 100%
}
body {
margin:0 3rem;
padding:0;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 1.5rem;
line-height: 1.4;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
}
.container {
margin: 0rem;
max-width: 600px;
padding-bottom:10rem;
}
header {
color: #000;
font-size: 4rem;
letter-spacing: 2px;
line-height: 1.1;
margin-bottom: 2rem;
}
p {
font-family: Optima, Segoe, "Segoe UI", Candara, Calibri, Arial, sans-serif;
color: #666;
}
h5 {
font-weight: normal;
color: #999;
font-size: 1rem;
}
h6 {
font-weight: normal;
color: #999;
}
code {
font-weight: bold;
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Layout template file for Whoops's pretty error output.
*/
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Whoops there was an error!</title>
<style><?php echo $stylesheet ?></style>
</head>
<body>
<div class="container">
<div class="details">
<header>
Server Error
</header>
<p>We're sorry! The server has encountered an internal error and was unable to complete your request.
Please contact the system administrator for more information.</p>
<h6>For further details please review your <code>logs/</code> folder, or enable displaying of errors in your system configuration.</h6>
<h6>Error Code: <b><?php echo $code ?></b></h6>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,91 @@
<?php
namespace Grav\Common\Errors;
use Whoops\Handler\Handler;
use Whoops\Util\Misc;
use Whoops\Util\TemplateHelper;
class SimplePageHandler extends Handler
{
private $searchPaths = array();
private $resourceCache = array();
public function __construct()
{
// Add the default, local resource search path:
$this->searchPaths[] = __DIR__ . "/Resources";
}
/**
* @return int|null
*/
public function handle()
{
$exception = $this->getException();
$inspector = $this->getInspector();
$run = $this->getRun();
$helper = new TemplateHelper();
$templateFile = $this->getResource("layout.html.php");
$cssFile = $this->getResource("error.css");
$code = $inspector->getException()->getCode();
if ($inspector->getException() instanceof \ErrorException) {
$code = Misc::translateErrorCode($code);
}
$vars = array(
"stylesheet" => file_get_contents($cssFile),
"code" => $code,
);
$helper->setVariables($vars);
$helper->render($templateFile);
return Handler::QUIT;
}
protected function getResource($resource)
{
// If the resource was found before, we can speed things up
// by caching its absolute, resolved path:
if (isset($this->resourceCache[$resource])) {
return $this->resourceCache[$resource];
}
// Search through available search paths, until we find the
// resource we're after:
foreach ($this->searchPaths as $path) {
$fullPath = $path . "/$resource";
if (is_file($fullPath)) {
// Cache the result:
$this->resourceCache[$resource] = $fullPath;
return $fullPath;
}
}
// If we got this far, nothing was found.
throw new RuntimeException(
"Could not find resource '$resource' in any resource paths."
. "(searched: " . join(", ", $this->searchPaths). ")"
);
}
public function addResourcePath($path)
{
if (!is_dir($path)) {
throw new InvalidArgumentException(
"'$path' is not a valid directory"
);
}
array_unshift($this->searchPaths, $path);
}
public function getResourcePaths()
{
return $this->searchPaths;
}
}

View File

@@ -161,10 +161,10 @@ class Grav extends Container
// Initialize configuration.
$debugger->startTimer('_config', 'Configuration');
$this['config']->init();
$debugger->stopTimer('_config');
$this['errors']->resetHandlers();
$debugger->init();
$this['config']->debug();
$debugger->stopTimer('_config');
$debugger->startTimer('streams', 'Streams');
$this['streams'];
@@ -186,7 +186,6 @@ class Grav extends Container
}
$this['assets']->init();
$this->fireEvent('onAssetsInitialized');
$debugger->startTimer('twig', 'Twig');
@@ -200,7 +199,6 @@ class Grav extends Container
$this->fireEvent('onPageInitialized');
$debugger->addAssets();
// Process whole page as required
@@ -209,7 +207,6 @@ class Grav extends Container
$this->fireEvent('onOutputGenerated');
$debugger->stopTimer('render');
// Set the header type
$this->header();
echo $this->output;
@@ -217,7 +214,6 @@ class Grav extends Container
$this->fireEvent('onOutputRendered');
register_shutdown_function([$this, 'shutdown']);
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Grav\Common\Service;
use Grav\Common\Errors\Errors;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Whoops\Handler\JsonResponseHandler;
@@ -12,8 +13,8 @@ class ErrorServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
// Setup Whoops error handler
$whoops = new Run;
// Setup Whoops-based error handler
$errors = new Errors;
$error_page = new PrettyPageHandler;
$error_page->setPageTitle('Crikey! There was an error...');
@@ -24,19 +25,17 @@ class ErrorServiceProvider implements ServiceProviderInterface
$json_page = new JsonResponseHandler;
$json_page->onlyForAjaxRequests(true);
$whoops->pushHandler($error_page);
$whoops->pushHandler(new PlainTextHandler);
$whoops->pushHandler($json_page);
$errors->pushHandler($error_page, 'pretty');
$errors->pushHandler(new PlainTextHandler, 'text');
$errors->pushHandler($json_page, 'json');
$logger = $container['log'];
$whoops->pushHandler(function ($exception, $inspector, $run) use($logger) {
$errors->pushHandler(function ($exception, $inspector, $run) use($logger) {
$logger->addCritical($exception->getMessage(). ' - Trace: '. $exception->getTraceAsString());
});
}, 'log');
$whoops->register();
$errors->register();
$container['whoops'] = $whoops;
$container['errors'] = $errors;
}
}

View File

@@ -28,6 +28,10 @@ assets:
js_pipeline: false
js_minify: true
errors:
display: true
log: true
debugger:
enabled: false
twig: true