This commit is contained in:
William 2022-01-27 23:17:43 +01:00
parent 692e07f070
commit 2f2e097db3
2 changed files with 48 additions and 0 deletions

44
app/core/ErrorHandler.php Normal file
View File

@ -0,0 +1,44 @@
<?php
// Janky error handler page thingy that looks cool
class ErrorHandler
{
public array $errors; // Holds error messages
public function __construct()
{
ob_Start(); // If an error were to occur, we clear the outputted data so we only show the error page instead of both.
set_error_handler([$this, 'error']);
set_exception_handler([$this, 'exception']);
$this->errors = [];
}
public function error($errno, $errstr, $errfile, $errline): void
{
$errstr = htmlspecialchars($errstr);
$this->errors[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
die();
}
public function exception($exception): void
{
$this->errors[] = "<b>Uncaught Exception:</b> " . $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 '<style>body { margin: 1rem; background: tomato; font-family: sans; } #error { padding: 1rem; background: white; margin-bottom: 1rem; border: .1rem solid black; } </style>';
echo '<h1>Error!!1 (✖﹏✖)</h1>';
echo '<p>Oisann! Dette var ikke ment å skje. Dersom det vedvarer, vennligst kontakt nettadministratoren.</p>';
foreach ($this->errors as $error) {
echo "<div id=\"error\">$error</div>";
}
}
}

View File

@ -20,10 +20,14 @@ spl_autoload_register(function ($class_name) {
require __DIR__ . '/core/' . $class_name . '.php'; require __DIR__ . '/core/' . $class_name . '.php';
}); });
// Error and exception handling
new ErrorHandler;
// Setup // Setup
$config = ( $config = (
new Config(__DIR__ . '/config.php') new Config(__DIR__ . '/config.php')
)->config; )->config;
$database = new Database($config['database']); $database = new Database($config['database']);
$session = new Session; $session = new Session;
$user = new User($session); $user = new User($session);