|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Error 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 Errors{ |
|
|
|
|
|
|
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__ . "::errorHandler"); |
|
34
|
|
|
set_exception_handler(__CLASS__ .'::exceptionHandler'); |
|
35
|
|
|
register_shutdown_function(__CLASS__ ."::fatalErrorHandler" ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Handle & log fatal errors |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function fatalErrorHandler(){ |
|
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::exceptionHandler(new Exception($error['message'], 500)); |
|
|
|
|
|
|
57
|
|
|
self::errorHandler($error['type'], $error['message'], $error['file'], $error['line'], null); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Handle & log errors |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
* @see http://php.net/manual/en/errorfunc.examples.php |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function errorHandler($errno, $errmsg, $filename, $linenum, $vars){ |
|
68
|
|
|
|
|
69
|
|
|
// set of errors for which a var trace will be saved |
|
70
|
|
|
$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE); |
|
71
|
|
|
|
|
72
|
|
|
$err = "<errorentry>\n"; |
|
73
|
|
|
$err .= "\t<errortype>" . self::errorType($errno) . "</errortype>\n"; |
|
74
|
|
|
$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n"; |
|
75
|
|
|
|
|
76
|
|
|
if (in_array($errno, $user_errors)) { |
|
77
|
|
|
$err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") . "</vartrace>\n"; |
|
78
|
|
|
} |
|
79
|
|
|
$err .= "</errorentry>\n\n"; |
|
80
|
|
|
|
|
81
|
|
|
Logger::Log("ERROR", $err, $filename, $linenum); |
|
82
|
|
|
self::viewError(); |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Handle & log exceptions |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
* @see http://php.net/manual/en/function.set-exception-handler.php |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function exceptionHandler($e) { |
|
93
|
|
|
|
|
94
|
|
|
Logger::Log(get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()); |
|
95
|
|
|
self::viewError(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Map an error code to error text |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $errno |
|
102
|
|
|
* @return string error text |
|
103
|
|
|
*/ |
|
104
|
|
|
private static function errorType($errno){ |
|
105
|
|
|
|
|
106
|
|
|
// define an assoc array of error string |
|
107
|
|
|
$errortype = array ( |
|
108
|
|
|
E_ERROR => 'Error', |
|
109
|
|
|
E_WARNING => 'Warning', |
|
110
|
|
|
E_PARSE => 'Parsing Error', |
|
111
|
|
|
E_NOTICE => 'Notice', |
|
112
|
|
|
E_CORE_ERROR => 'Core Error', |
|
113
|
|
|
E_CORE_WARNING => 'Core Warning', |
|
114
|
|
|
E_COMPILE_ERROR => 'Compile Error', |
|
115
|
|
|
E_COMPILE_WARNING => 'Compile Warning', |
|
116
|
|
|
E_USER_ERROR => 'User Error', |
|
117
|
|
|
E_USER_WARNING => 'User Warning', |
|
118
|
|
|
E_USER_NOTICE => 'User Notice', |
|
119
|
|
|
E_STRICT => 'Runtime Notice', |
|
120
|
|
|
E_RECOVERABLE_ERROR => 'Catchable Fatal Error' |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
return $errortype[$errno]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* display system error page as result or error or exception |
|
128
|
|
|
* |
|
129
|
|
|
*/ |
|
130
|
|
|
private static function viewError(){ |
|
131
|
|
|
|
|
132
|
|
|
$controller = new ErrorsController(); |
|
133
|
|
|
$controller->error("system"); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
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.