/
class.errorhandler.php
332 lines (290 loc) · 9.68 KB
/
class.errorhandler.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* @package core
*/
require_once(CORE . '/class.log.php');
/**
* GenericExceptionHandler will handle any uncaught exceptions thrown in
* Symphony. Additionally, all errors in Symphony that are raised to Exceptions
* will be handled by this class.
* It is possible for Exceptions to be caught by their own `ExceptionHandler` which can
* override the `render` function so that it can be displayed to the user appropriately.
*/
Class GenericExceptionHandler {
/**
* Whether the `GenericExceptionHandler` should handle exceptions. Defaults to true
* @var boolean
*/
public static $enabled = true;
/**
* An instance of the Symphony Log class, used to write errors to the log
* @var Log
*/
private static $_Log = null;
/**
* Initialise will set the error handler to be the `__CLASS__::handler` function.
*
* @param Log $log
* An instance of a Symphony Log object to write errors to
*/
public static function initialise(Log $Log = null){
if(!is_null($Log)){
self::$_Log = $Log;
}
set_exception_handler(array(__CLASS__, 'handler'));
}
/**
* Retrieves a window of lines before and after the line where the error
* occurred so that a developer can help debug the exception
*
* @param integer $line
* The line where the error occurred.
* @param string $file
* The file that holds the logic that caused the error.
* @param integer $window
* The number of lines either side of the line where the error occurred
* to show
* @return array
*/
protected static function __nearbyLines($line, $file, $window=5){
if( !file_exists($file) ) return array();
return array_slice(file($file), ($line - 1) - $window, $window * 2, true);
}
/**
* The handler function is given an Exception and will call it's render
* function to display the Exception to a user. After calling the render
* function, the output is displayed and then exited to prevent any further
* logic from occurring.
*
* @param Exception $e
* The Exception object
* @return string
* The result of the Exception's render function
*/
public static function handler(Exception $e){
try{
// Instead of just throwing an empty page, return a 404 page.
if(self::$enabled !== true){
require_once(CORE . '/class.frontend.php');
$e = new FrontendPageNotFoundException();
};
$exception_type = get_class($e);
if(class_exists("{$exception_type}Handler") && method_exists("{$exception_type}Handler", 'render')){
$class = "{$exception_type}Handler";
}
else {
$class = __CLASS__;
}
// Exceptions should be logged if they are not caught.
if(self::$_Log instanceof Log) {
self::$_Log->pushExceptionToLog($e, true);
}
if(!headers_sent()) {
Page::renderStatusCode(Page::HTTP_STATUS_ERROR);
header('Content-Type: text/html; charset=utf-8');
}
$output = call_user_func(array($class, 'render'), $e);
echo $output;
exit;
}
catch(Exception $e){
try {
if(!headers_sent()) {
Page::renderStatusCode(Page::HTTP_STATUS_ERROR);
header('Content-Type: text/html; charset=utf-8');
}
$output = call_user_func(array('GenericExceptionHandler', 'render'), $e);
echo $output;
exit;
}
catch(Exception $e) {
echo "<pre>";
echo 'A severe error occurred whilst trying to handle an exception:' . PHP_EOL;
echo $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile();
exit;
}
}
}
/**
* Returns the path to the error-template by looking at the
* `WORKSPACE/template/` directory, then at the `TEMPLATES`
* directory for the convention `*.tpl`. If the template
* is not found, false is returned
*
* @since Symphony 2.3
* @param string $name
* Name of the template
* @return mixed
* String, which is the path to the template if the template is found,
* false otherwise
*/
public static function getTemplate($name) {
$format = '%s/%s.tpl';
if(file_exists($template = sprintf($format, WORKSPACE . '/template', $name)))
return $template;
elseif(file_exists($template = sprintf($format, TEMPLATE, $name)))
return $template;
else
return false;
}
/**
* The render function will take an Exception and output a HTML page
*
* @param Exception $e
* The Exception object
* @return string
* An HTML string
*/
public static function render(Exception $e){
$lines = NULL;
foreach(self::__nearByLines($e->getLine(), $e->getFile()) as $line => $string){
$lines .= sprintf(
'<li%s><strong>%d</strong> <code>%s</code></li>',
(($line+1) == $e->getLine() ? ' class="error"' : NULL),
++$line,
str_replace("\t", ' ', htmlspecialchars($string))
);
}
$trace = NULL;
foreach($e->getTrace() as $t){
$trace .= sprintf(
'<li><code><em>[%s:%d]</em></code></li><li><code>    %s%s%s();</code></li>',
(isset($t['file']) ? $t['file'] : NULL),
(isset($t['line']) ? $t['line'] : NULL),
(isset($t['class']) ? $t['class'] : NULL),
(isset($t['type']) ? $t['type'] : NULL),
$t['function']
);
}
$queries = NULL;
if(is_object(Symphony::Database())){
$debug = Symphony::Database()->debug();
if(!empty($debug)) foreach($debug as $query){
$queries .= sprintf(
'<li><em>[%01.4f]</em><code> %s;</code> </li>',
(isset($query['execution_time']) ? $query['execution_time'] : NULL),
htmlspecialchars($query['query'])
);
}
}
$html = sprintf(file_get_contents(self::getTemplate('fatalerror.generic')),
($e instanceof ErrorException ? GenericErrorHandler::$errorTypeStrings[$e->getSeverity()] : 'Fatal Error'),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$lines,
$trace,
$queries
);
$html = str_replace('{SYMPHONY_URL}', SYMPHONY_URL, $html);
$html = str_replace('{URL}', URL, $html);
return $html;
}
}
/**
* `GenericErrorHandler` will catch any warnings or notices thrown by PHP and
* raise the errors to Exceptions so they can be dealt with by the
* `GenericExceptionHandler`. The type of errors that are raised to Exceptions
* depends on the `error_reporting` level. All errors raised, except
* `E_NOTICE` and `E_STRICT` are written to the Symphony log.
*/
Class GenericErrorHandler {
/**
* Whether the error handler is enabled or not, defaults to true.
* Setting to false will prevent any Symphony error handling from occurring
* @var boolean
*/
public static $enabled = true;
/**
* An instance of the Log class, used to write errors to the log
* @var Log
*/
private static $_Log = null;
/**
* Whether to log errors or not.
* This one is to be used temporarily, e.g., when PHP function is
* supposed to error out and throw warning and log should be kept clean.
*
* @since Symphony 2.2.2
* @var boolean
* @example
* GenericErrorHandler::$logDisabled = true;
* DoSomethingThatEndsWithWarningsYouDoNotWantInLogs();
* GenericErrorHandler::$logDisabled = false;
*/
public static $logDisabled = false;
/**
* An associative array with the PHP error constant as a key, and
* a string describing that constant as the value
* @var array
*/
public static $errorTypeStrings = array (
E_NOTICE => 'Notice',
E_WARNING => 'Warning',
E_ERROR => 'Error',
E_PARSE => 'Parsing Error',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_NOTICE => 'User Notice',
E_USER_WARNING => 'User Warning',
E_USER_ERROR => 'User Error',
E_STRICT => 'Strict Notice',
E_RECOVERABLE_ERROR => 'Recoverable Error',
E_DEPRECATED => 'Deprecated Warning'
);
/**
* Initialise will set the error handler to be the `__CLASS__::handler`
* function.
*
* @param Log|null $Log (optional)
* An instance of a Symphony Log object to write errors to
*/
public static function initialise(Log $Log = null){
if(!is_null($Log)){
self::$_Log = $Log;
}
set_error_handler(array(__CLASS__, 'handler'), error_reporting());
}
/**
* Determines if the error handler is enabled by checking that error_reporting
* is set in the php config and that $enabled is true
*
* @return boolean
*/
public static function isEnabled(){
return (bool)error_reporting() && self::$enabled;
}
/**
* The handler function will write the error to the `$Log` if it is not `E_NOTICE`
* or `E_STRICT` before raising the error as an Exception. This allows all `E_WARNING`
* to actually be captured by an Exception handler.
*
* @param integer $code
* The error code, one of the PHP error constants
* @param string $message
* The message of the error, this will be written to the log and
* displayed as the exception message
* @param string $file
* The file that holds the logic that caused the error. Defaults to null
* @param integer $line
* The line where the error occurred.
* @return string
* Usually a string of HTML that will displayed to a user
*/
public static function handler($code, $message, $file = null, $line = null){
// Only log if the error won't be raised to an exception and the error is not `E_STRICT`
if(!self::$logDisabled && !in_array($code, array(E_STRICT)) && self::$_Log instanceof Log){
self::$_Log->pushToLog(
sprintf(
'%s %s: %s%s%s',
__CLASS__, $code, $message, ($line ? " on line $line" : null), ($file ? " of file $file" : null)
), $code, true
);
}
if(self::isEnabled()) {
throw new ErrorException($message, 0, $code, $file, $line);
}
}
}