mirror of
https://github.com/getgrav/grav.git
synced 2025-12-08 00:39:58 +01:00
Added ability to enable/disable error display and logging
This commit is contained in:
@@ -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_pipeline: false # The JS pipeline is the unification of multiple JS resources into one file
|
||||||
js_minify: true # Minify the JS during pipelining
|
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:
|
debugger:
|
||||||
enabled: false # Enable Grav debugger and following settings
|
enabled: false # Enable Grav debugger and following settings
|
||||||
twig: true # Enable debugging of Twig templates
|
twig: true # Enable debugging of Twig templates
|
||||||
|
|||||||
57
system/src/Grav/Common/Errors/Errors.php
Normal file
57
system/src/Grav/Common/Errors/Errors.php
Normal 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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
50
system/src/Grav/Common/Errors/Resources/error.css
Normal file
50
system/src/Grav/Common/Errors/Resources/error.css
Normal 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;
|
||||||
|
}
|
||||||
26
system/src/Grav/Common/Errors/Resources/layout.html.php
Normal file
26
system/src/Grav/Common/Errors/Resources/layout.html.php
Normal 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>
|
||||||
91
system/src/Grav/Common/Errors/SimplePageHandler.php
Normal file
91
system/src/Grav/Common/Errors/SimplePageHandler.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -161,10 +161,10 @@ class Grav extends Container
|
|||||||
// Initialize configuration.
|
// Initialize configuration.
|
||||||
$debugger->startTimer('_config', 'Configuration');
|
$debugger->startTimer('_config', 'Configuration');
|
||||||
$this['config']->init();
|
$this['config']->init();
|
||||||
$debugger->stopTimer('_config');
|
$this['errors']->resetHandlers();
|
||||||
|
|
||||||
$debugger->init();
|
$debugger->init();
|
||||||
$this['config']->debug();
|
$this['config']->debug();
|
||||||
|
$debugger->stopTimer('_config');
|
||||||
|
|
||||||
$debugger->startTimer('streams', 'Streams');
|
$debugger->startTimer('streams', 'Streams');
|
||||||
$this['streams'];
|
$this['streams'];
|
||||||
@@ -186,7 +186,6 @@ class Grav extends Container
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this['assets']->init();
|
$this['assets']->init();
|
||||||
|
|
||||||
$this->fireEvent('onAssetsInitialized');
|
$this->fireEvent('onAssetsInitialized');
|
||||||
|
|
||||||
$debugger->startTimer('twig', 'Twig');
|
$debugger->startTimer('twig', 'Twig');
|
||||||
@@ -200,7 +199,6 @@ class Grav extends Container
|
|||||||
|
|
||||||
$this->fireEvent('onPageInitialized');
|
$this->fireEvent('onPageInitialized');
|
||||||
|
|
||||||
|
|
||||||
$debugger->addAssets();
|
$debugger->addAssets();
|
||||||
|
|
||||||
// Process whole page as required
|
// Process whole page as required
|
||||||
@@ -209,7 +207,6 @@ class Grav extends Container
|
|||||||
$this->fireEvent('onOutputGenerated');
|
$this->fireEvent('onOutputGenerated');
|
||||||
$debugger->stopTimer('render');
|
$debugger->stopTimer('render');
|
||||||
|
|
||||||
|
|
||||||
// Set the header type
|
// Set the header type
|
||||||
$this->header();
|
$this->header();
|
||||||
echo $this->output;
|
echo $this->output;
|
||||||
@@ -217,7 +214,6 @@ class Grav extends Container
|
|||||||
|
|
||||||
$this->fireEvent('onOutputRendered');
|
$this->fireEvent('onOutputRendered');
|
||||||
|
|
||||||
|
|
||||||
register_shutdown_function([$this, 'shutdown']);
|
register_shutdown_function([$this, 'shutdown']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Grav\Common\Service;
|
namespace Grav\Common\Service;
|
||||||
|
|
||||||
|
use Grav\Common\Errors\Errors;
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
use Whoops\Handler\JsonResponseHandler;
|
use Whoops\Handler\JsonResponseHandler;
|
||||||
@@ -12,8 +13,8 @@ class ErrorServiceProvider implements ServiceProviderInterface
|
|||||||
{
|
{
|
||||||
public function register(Container $container)
|
public function register(Container $container)
|
||||||
{
|
{
|
||||||
// Setup Whoops error handler
|
// Setup Whoops-based error handler
|
||||||
$whoops = new Run;
|
$errors = new Errors;
|
||||||
|
|
||||||
$error_page = new PrettyPageHandler;
|
$error_page = new PrettyPageHandler;
|
||||||
$error_page->setPageTitle('Crikey! There was an error...');
|
$error_page->setPageTitle('Crikey! There was an error...');
|
||||||
@@ -24,19 +25,17 @@ class ErrorServiceProvider implements ServiceProviderInterface
|
|||||||
$json_page = new JsonResponseHandler;
|
$json_page = new JsonResponseHandler;
|
||||||
$json_page->onlyForAjaxRequests(true);
|
$json_page->onlyForAjaxRequests(true);
|
||||||
|
|
||||||
|
$errors->pushHandler($error_page, 'pretty');
|
||||||
$whoops->pushHandler($error_page);
|
$errors->pushHandler(new PlainTextHandler, 'text');
|
||||||
$whoops->pushHandler(new PlainTextHandler);
|
$errors->pushHandler($json_page, 'json');
|
||||||
$whoops->pushHandler($json_page);
|
|
||||||
|
|
||||||
|
|
||||||
$logger = $container['log'];
|
$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());
|
$logger->addCritical($exception->getMessage(). ' - Trace: '. $exception->getTraceAsString());
|
||||||
});
|
}, 'log');
|
||||||
|
|
||||||
$whoops->register();
|
$errors->register();
|
||||||
|
|
||||||
$container['whoops'] = $whoops;
|
$container['errors'] = $errors;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ assets:
|
|||||||
js_pipeline: false
|
js_pipeline: false
|
||||||
js_minify: true
|
js_minify: true
|
||||||
|
|
||||||
|
errors:
|
||||||
|
display: true
|
||||||
|
log: true
|
||||||
|
|
||||||
debugger:
|
debugger:
|
||||||
enabled: false
|
enabled: false
|
||||||
twig: true
|
twig: true
|
||||||
|
|||||||
Reference in New Issue
Block a user