This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/core/ErrorHandler.php

46 lines
1.6 KiB
PHP
Raw Normal View History

2022-01-27 22:17:43 +00:00
<?php
2022-02-28 03:14:53 +00:00
// Bit of a janky way to display a custom page on error or exception.
// But looks pretty cool and professional!
2022-01-27 22:17:43 +00:00
class ErrorHandler
{
2022-02-23 17:40:49 +00:00
public array $errors; // Holds error messages (even though for now we only display a maximum of 1)
2022-01-27 22:17:43 +00:00
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);
2022-02-10 16:57:39 +00:00
header_remove();
2022-01-27 22:17:43 +00:00
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>";
}
}
}