api/Core/Error.php

86 lines
2.5 KiB
PHP
Raw Normal View History

2019-03-30 12:19:46 +00:00
<?php
namespace Core;
/**
* Error and exception handler
*
* PHP version 7.0
*/
class Error
{
2021-02-23 12:46:33 +00:00
/**
* Error handler. Convert all errors to Exceptions by throwing an ErrorException.
*
* @param int $level Error level
* @param string $message Error message
* @param string $file Filename the error was raised in
* @param int $line Line number in the file
*
* @return void
*/
public static function errorHandlerNone($level, $message, $file, $line)
{
2021-02-28 10:02:23 +00:00
// echo "Handler captured error $level: '$message'" . PHP_EOL;
2021-02-23 12:46:33 +00:00
}
2019-03-30 12:19:46 +00:00
/**
* Error handler. Convert all errors to Exceptions by throwing an ErrorException.
*
* @param int $level Error level
* @param string $message Error message
* @param string $file Filename the error was raised in
* @param int $line Line number in the file
*
* @return void
*/
public static function errorHandler($level, $message, $file, $line)
{
if (error_reporting() !== 0) { // to keep the @ operator working
throw new \ErrorException($message, 0, $level, $file, $line);
}
}
/**
* Exception handler.
*
* @param Exception $exception The exception
*
* @return void
*/
public static function exceptionHandler($exception)
{
// Code is 404 (not found) or 500 (general error)
$code = $exception->getCode();
if ($code != 404) {
$code = 500;
}
http_response_code($code);
if (\App\Config::SHOW_ERRORS) {
echo "<h1>Fatal error</h1>";
echo "<p>Uncaught exception: '" . get_class($exception) . "'</p>";
echo "<p>Message: '" . $exception->getMessage() . "'</p>";
echo "<p>Stack trace:<pre>" . $exception->getTraceAsString() . "</pre></p>";
echo "<p>Thrown in '" . $exception->getFile() . "' on line " . $exception->getLine() . "</p>";
} else {
$log = dirname(__DIR__) . '/logs/' . date('Y-m-d') . '.txt';
ini_set('error_log', $log);
$message = "Uncaught exception: '" . get_class($exception) . "'";
$message .= " with message '" . $exception->getMessage() . "'";
$message .= "\nStack trace: " . $exception->getTraceAsString();
$message .= "\nThrown in '" . $exception->getFile() . "' on line " . $exception->getLine();
error_log($message);
View::renderTemplate("$code.html");
}
}
2021-02-23 12:46:33 +00:00
public static function exceptionHandlerNone($exception){
}
2019-03-30 12:19:46 +00:00
}