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

@@ -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.');
}
}