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);