Add global exception handler and debug option to display detailed information

This commit is contained in:
Dale Davies
2023-04-08 17:29:40 +01:00
parent 0c2c320c75
commit 7a11b8ccd8
7 changed files with 121 additions and 25 deletions

View File

@@ -14,6 +14,7 @@
namespace Jump;
use \Jump\Exceptions\ConfigException;
use \Jump\Pages\ErrorPage;
/**
* Defines a class for loading language strings form available translations files, caching
@@ -64,7 +65,7 @@ class Language {
try {
$locale = new \Utopia\Locale\Locale($this->config->get('language'));
} catch (\Exception) {
(new Pages\ErrorPage($this->cache, $this->config, 500, 'Provided language code has no corresponding translation file.'))->init();
ErrorPage::display($this->config, 500, 'Provided language code has no corresponding translation file.');
}
$this->locale = $locale;

View File

@@ -13,6 +13,9 @@
namespace Jump;
use \Jump\Pages\ErrorPage;
use \Tracy\Debugger;
class Main {
private Cache $cache;
@@ -23,7 +26,18 @@ class Main {
private \Nette\Http\Session $session;
public function __construct() {
// We can't do anything without the config object.
$this->config = new Config();
// Set something to either display detailed debugging info or handle exceptions
// as early as possible during initialisation.
if ($this->config->get('debug')) {
Debugger::enable(Debugger::Development);
} else {
set_exception_handler([$this, 'exception_handler']);
}
// Carry on setting things up.
$this->cache = new Cache($this->config);
$this->router = new \Nette\Routing\RouteList;
$this->language = new Language($this->config, $this->cache);
@@ -49,7 +63,7 @@ class Main {
]);
}
function init() {
public function init() {
// Create a request object based on globals so we can utilise url rewriting etc.
$this->request = (new \Nette\Http\RequestFactory)->fromGlobals();
@@ -70,4 +84,15 @@ class Main {
return $page->get_output();
}
/**
* Global exception handler, display friendly message if something goes wrong.
*
* @param $exception
* @return void
*/
public function exception_handler($exception): void {
error_log($exception->getMessage());
ErrorPage::display($this->config, 500, 'Something went wrong, please use debug option to see details.');
}
}

View File

@@ -14,26 +14,17 @@
namespace Jump\Pages;
class ErrorPage {
private string $content;
public function __construct(private \Jump\Cache $cache, private \Jump\Config $config, private int $httpcode, public string $message) {
$this->mustache = new \Mustache_Engine([
'loader' => new \Mustache_Loader_FilesystemLoader($this->config->get('templatedir'))
public static function display(\Jump\Config $config, int $httpcode, string $message) {
$mustache = new \Mustache_Engine([
'loader' => new \Mustache_Loader_FilesystemLoader($config->get('templatedir'))
]);
$this->content = $cache->load(cachename: 'templates/errorpage', key: $httpcode.md5($message), callback: function() use ($httpcode, $message) {
$template = $this->mustache->loadTemplate('errorpage');
return $template->render([
'code' => $httpcode,
'message' => $message,
'wwwurl' => $this->config->get_wwwurl(),
]);
});
$template = $mustache->loadTemplate('errorpage');
$content = $template->render([
'code' => $httpcode,
'message' => $message,
'wwwurl' => $config->get_wwwurl(),
]);
http_response_code($httpcode);
die($content);
}
public function init() {
http_response_code($this->httpcode);
die($this->content);
}
}

View File

@@ -63,7 +63,7 @@ class TagPage extends AbstractPage {
$taggedsites = $sites->get_sites_by_tag($this->tagname);
}
catch (TagNotFoundException) {
(new ErrorPage($this->cache, $this->config, 404, 'There are no sites with this tag.'))->init();
ErrorPage::display($this->config, 404, 'There are no sites with this tag.');
}
$template = $this->mustache->loadTemplate('sites');
return $template->render([

View File

@@ -20,6 +20,7 @@
"guzzlehttp/guzzle": "^7.0",
"unsplash/unsplash": "3.2.1",
"divineomega/array_undot": "^4.1",
"utopia-php/locale": "^0.6.0"
"utopia-php/locale": "^0.6.0",
"tracy/tracy": "^2.10"
}
}

76
jumpapp/composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "5fcb97502e7722b9364b85cdd3a4f851",
"content-hash": "124fb3f5325190e7a3c8284b42e9e304",
"packages": [
{
"name": "arthurhoaro/favicon",
@@ -1502,6 +1502,80 @@
],
"time": "2023-02-21T19:46:44+00:00"
},
{
"name": "tracy/tracy",
"version": "v2.10.2",
"source": {
"type": "git",
"url": "https://github.com/nette/tracy.git",
"reference": "882fee7cf4258a602ad4a37461e837ed2ca1406b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nette/tracy/zipball/882fee7cf4258a602ad4a37461e837ed2ca1406b",
"reference": "882fee7cf4258a602ad4a37461e837ed2ca1406b",
"shasum": ""
},
"require": {
"ext-json": "*",
"ext-session": "*",
"php": ">=8.0 <8.3"
},
"conflict": {
"nette/di": "<3.0"
},
"require-dev": {
"latte/latte": "^2.5",
"nette/di": "^3.0",
"nette/mail": "^3.0",
"nette/tester": "^2.2",
"nette/utils": "^3.0",
"phpstan/phpstan": "^1.0",
"psr/log": "^1.0 || ^2.0 || ^3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.10-dev"
}
},
"autoload": {
"files": [
"src/Tracy/functions.php"
],
"classmap": [
"src"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "David Grudl",
"homepage": "https://davidgrudl.com"
},
{
"name": "Nette Community",
"homepage": "https://nette.org/contributors"
}
],
"description": "😎 Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.",
"homepage": "https://tracy.nette.org",
"keywords": [
"Xdebug",
"debug",
"debugger",
"nette",
"profiler"
],
"support": {
"issues": "https://github.com/nette/tracy/issues",
"source": "https://github.com/nette/tracy/tree/v2.10.2"
},
"time": "2023-03-29T12:34:53+00:00"
},
{
"name": "unsplash/unsplash",
"version": "3.2.1",

View File

@@ -29,6 +29,10 @@ return [
// Where is the cache storage directory, should not be public.
'cachedir' => getenv('CACHEDIR') ?: '/var/www/cache',
// Soemthing not working? Set this to "true" to display detailed
// debugging information.
'debug' => getenv('DEBUG') ?: false,
// Display alternative layout of sites list.
'altlayout' => getenv('ALTLAYOUT') ?: false,
// Should the clock be displayed?