From 2f2e097db3e2ab222930811fd06b539d28a92f59 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 27 Jan 2022 23:17:43 +0100 Subject: [PATCH] Commit --- app/core/ErrorHandler.php | 44 +++++++++++++++++++++++++++++++++++++++ app/inc.php | 4 ++++ 2 files changed, 48 insertions(+) create mode 100644 app/core/ErrorHandler.php diff --git a/app/core/ErrorHandler.php b/app/core/ErrorHandler.php new file mode 100644 index 0000000..3e75063 --- /dev/null +++ b/app/core/ErrorHandler.php @@ -0,0 +1,44 @@ +errors = []; + } + + public function error($errno, $errstr, $errfile, $errline): void + { + $errstr = htmlspecialchars($errstr); + $this->errors[] = "Error[$errno]: $errstr in $errfile at line $errline"; + die(); + } + + public function exception($exception): void + { + $this->errors[] = "Uncaught Exception: " . $exception; + } + + public function __destruct() + { + if (!$this->errors) { + return; + } + ob_end_clean(); // Remove current output to be replaced by error page + http_response_code(500); + + echo ''; + echo '

Error!!1 (✖﹏✖)

'; + echo '

Oisann! Dette var ikke ment å skje. Dersom det vedvarer, vennligst kontakt nettadministratoren.

'; + + foreach ($this->errors as $error) { + echo "
$error
"; + } + } +} \ No newline at end of file diff --git a/app/inc.php b/app/inc.php index 0cc2b2d..14d9137 100644 --- a/app/inc.php +++ b/app/inc.php @@ -20,10 +20,14 @@ spl_autoload_register(function ($class_name) { require __DIR__ . '/core/' . $class_name . '.php'; }); +// Error and exception handling +new ErrorHandler; + // Setup $config = ( new Config(__DIR__ . '/config.php') )->config; + $database = new Database($config['database']); $session = new Session; $user = new User($session);