|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Handler class. |
|
5
|
|
|
* |
|
6
|
|
|
* Provides basic error and exception handling for your application. |
|
7
|
|
|
* It captures and handles all unhandled exceptions and errors. |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
10
|
|
|
* @author Omar El Gabry <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Handler{ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Constructor |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
private function __construct(){} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Register the error and exception handlers. |
|
23
|
|
|
* Must be called at the beginning of your application |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function register(){ |
|
28
|
|
|
|
|
29
|
|
|
// turn off all error reporting as well, |
|
30
|
|
|
// because we will take care of it |
|
31
|
|
|
error_reporting(0); |
|
32
|
|
|
|
|
33
|
|
|
set_error_handler(__CLASS__ . "::handleError"); |
|
34
|
|
|
set_exception_handler(__CLASS__ .'::handleException'); |
|
35
|
|
|
register_shutdown_function(__CLASS__ ."::handleFatalError" ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Handle fatal errors |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function handleFatalError(){ |
|
44
|
|
|
|
|
45
|
|
|
if (PHP_SAPI === 'cli') { return; } |
|
46
|
|
|
$error = error_get_last(); |
|
47
|
|
|
|
|
48
|
|
|
if (!is_array($error)) { return; } |
|
49
|
|
|
|
|
50
|
|
|
$fatals = [E_USER_ERROR, E_ERROR, E_PARSE]; |
|
51
|
|
|
|
|
52
|
|
|
if (!in_array($error['type'], $fatals, true)) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// self::handleError($error['type'], $error['message'], $error['file'], $error['line'], null); |
|
|
|
|
|
|
57
|
|
|
self::handleException(new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'])); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Handle errors |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
* @throws ErrorException |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function handleError($errno, $errmsg, $filename, $linenum, $vars){ |
|
|
|
|
|
|
67
|
|
|
throw new ErrorException($errmsg, 0, $errno, $filename, $linenum); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Handle & log exceptions |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
* @see http://php.net/manual/en/function.set-exception-handler.php |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function handleException($e) { |
|
77
|
|
|
Logger::Log(get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()); |
|
78
|
|
|
self::render($e); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* display system error page as result of an error or exception |
|
83
|
|
|
* |
|
84
|
|
|
* @param Exception $e |
|
85
|
|
|
* @return Response |
|
86
|
|
|
*/ |
|
87
|
|
|
private static function render(Exception $e){ |
|
88
|
|
|
|
|
89
|
|
|
if($e->getCode() === 400){ |
|
90
|
|
|
return (new ErrorsController())->error(400)->send(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return (new ErrorsController())->error(500)->send(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Map an error code to error text |
|
98
|
|
|
* |
|
99
|
|
|
* @param int $errno |
|
100
|
|
|
* @return string error text |
|
101
|
|
|
*/ |
|
102
|
|
|
private static function errorType($errno){ |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
// define an assoc array of error string |
|
105
|
|
|
$errortype = array ( |
|
106
|
|
|
E_ERROR => 'Error', |
|
107
|
|
|
E_WARNING => 'Warning', |
|
108
|
|
|
E_PARSE => 'Parsing Error', |
|
109
|
|
|
E_NOTICE => 'Notice', |
|
110
|
|
|
E_CORE_ERROR => 'Core Error', |
|
111
|
|
|
E_CORE_WARNING => 'Core Warning', |
|
112
|
|
|
E_COMPILE_ERROR => 'Compile Error', |
|
113
|
|
|
E_COMPILE_WARNING => 'Compile Warning', |
|
114
|
|
|
E_USER_ERROR => 'User Error', |
|
115
|
|
|
E_USER_WARNING => 'User Warning', |
|
116
|
|
|
E_USER_NOTICE => 'User Notice', |
|
117
|
|
|
E_STRICT => 'Runtime Notice', |
|
118
|
|
|
E_RECOVERABLE_ERROR => 'Catchable Fatal Error' |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
return $errortype[$errno]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.