-
Notifications
You must be signed in to change notification settings - Fork 0
/
Error.php
52 lines (47 loc) · 1.4 KB
/
Error.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Wrapper;
/**
* Error and exception handler
*
* PHP version 7.0
*/
class Error
{
/**
* 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);
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>";
}
}