-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemulator-errors.php
390 lines (376 loc) · 10.5 KB
/
emulator-errors.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
#FIXME: call_user_func* should not be in backtrace
use PhpParser\Node;
class EmulatedException extends Exception {
public $object=null;
function __construct(EmulatorObject $e)
{
$this->object=$e;
}
}
trait EmulatorErrors
{
/**
* The emulator throw.
* If a try/catch is active, will throw, otherwise will use exception handler
* @param Exception/Error $e
*/
function throw_exception($e)
{
$class=get_class($e);
$this->verbose("Throwing '{$class}' at {$this->filename_only()}:{$this->current_line} (try depth: {$this->try})...\n",4);
if (!$e instanceof Exception)
{
$this->verbose("Exception is user-type, wrapping into EmulatedException...\n",5);
if ($this->is_a($e,"Exception"))
$e=new EmulatedException($e);
else
$this->error("Inconsistency: exception of type unrelated to Exception found");
}
if ($this->try>0)
throw $e;
else
$this->exception_handler($e);
}
/**
* The depth of error suppression
* @var integer
*/
protected $error_suppression=0;
/**
* Suppress errors one more level
*/
function error_silence()
{
$this->error_suppression++;
}
/**
* Remove error suppression
*/
function error_restore()
{
$this->error_suppression--;
}
public $exception_handlers=[];
/**
* Default emulator exception handler
* @param Exception $e
* @return [description]
*/
public function exception_handler($e)
{
if (count($this->exception_handlers))
{
$this->call_function(end($this->exception_handlers),[$e]);
$this->terminated=true;
return true;
}
// return $this->error_handler($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
//program output
$this->output("PHP Fatal error: Uncaught Error: ".$e->getMessage()," in ",$this->current_file,":",$this->current_line,PHP_EOL);
$this->output("Stack trace:\n");
$backtrace=$this->print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->output($backtrace);
$count=count($this->trace);
$this->output("#",$count," {main}",PHP_EOL);
$this->output(" thrown in ",$this->current_file," on line ",$this->current_line,PHP_EOL);
$this->termination_value=-1;
$this->terminated=true;
//emulator output
// $this->verbose("PHP-Emul Fatal Error: Uncaught ".$e,0);
$this->verbose("PHP-Emul Fatal error: Uncaught Error: ".$e->getMessage()." in ".$e->getFile().":".$e->getLine()."\n",0);
if ($this->verbose>=2)
{
$this->verbose("Emulator Backtrace:\n");
echo $e->getTraceAsString(),PHP_EOL;
$this->verbose("Emulation Backtrace:\n");
echo $this->print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
return true;
}
/**
* Compatible with PHP
* @return true
*/
public function restore_exception_handler()
{
if (count($this->exception_handlers))
array_pop($this->exception_handlers);
return true;
}
/**
* Compatible with PHP
* @param mixed $handler
*/
public function set_exception_handler($handler)
{
if (count($this->exception_handlers))
$res=end($this->exception_handlers);
else
$res=null;
if (!$this->is_callable($handler)) return null;
$this->exception_handlers[]=$handler;
return $res;
}
/**
* Retains error_reporting value
* @var integer
*/
protected $error_reporting=-1;
/**
* Same as PHP's error_reporting
* @param int $level
* @return int
*/
public function error_reporting($level=null)
{
if ($level===null)
return $this->error_reporting;
$r=$this->error_reporting;
$this->error_reporting=$level;
return $r;
}
/**
* Used to output args of debug_print_backtrace
* @param [type] $obj [description]
* @return [type] [description]
*/
private function object_to_array($obj)
{
if(is_object($obj))
$new = (array) $obj;
elseif(is_array($obj))
{
$new = array();
foreach($obj as $key => $val)
$new[$key] = $this->object_to_array($val);
}
else $new = $obj;
return $new;
}
/**
* Returns backtrace equal to that of debug_backtrace
* @param int $options
* @param integer $limit
* @return array
*/
function backtrace($options=DEBUG_BACKTRACE_PROVIDE_OBJECT,$limit=0)
{
#TODO; possible options values : DEBUG_BACKTRACE_PROVIDE_OBJECT, DEBUG_BACKTRACE_IGNORE_ARGS
#TODO: this returns EmulatorObject
$t=$this->trace;
while (count($t) and (end($t)->function=="debug_backtrace" or end($t)->function=="debug_print_backtrace") )
array_pop($t);
$t=array_reverse($t);
if (! ($options&DEBUG_BACKTRACE_PROVIDE_OBJECT))
return $this->object_to_array($t);
return $t;
}
/**
* Prints the backtrace equal to debug_print_backtrace
* This function does not actually print the backtrace, it just returns it as a string.
* @param int $options
* @param integer $limit
* @return string backtrace print.
*/
function print_backtrace($options=DEBUG_BACKTRACE_PROVIDE_OBJECT ,$limit=0)
{
$noArgs=($options&DEBUG_BACKTRACE_IGNORE_ARGS);
$trace=$this->backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT,$limit);
$count=count($trace);
$out="";
for ($i=0;$i<$count;++$i)
{
$t=$trace[$i];
$function=$args=$file=$line=$class="";
if (isset($t->function))
$function=$t->function;
if (isset($t->class))
$class=$t->class;
if ($t->type) //not function, method or static-method
$function=$class.$t->type.$function;
if (isset($t->file))
$file=$t->file;
if (isset($t->line))
$line=$t->line;
if ( isset($t->args))
if (!$noArgs or $function=="require_once" or $function=="include_once" or $function=="include" or $function=="require")
$args=implode(", ",array_map(
function ($x){
$t=gettype($x);
if ($t=="boolean" or $t=="integer" or $t=="duoble" or $t=="string")
return $x;
else
return $t;
}
,$t->args));
$out.=sprintf ("#%d %s(%s) called at [%s:%d]\n",$i,$function,$args,$file,$line);
}
return $out;
}
public $error_handlers=[];
/**
* Equivalent to PHP's set_error_handler
* @param callable $handler
* @param int $error_reporting
* @return mixed
*/
function set_error_handler($handler,$error_reporting=32767)
{
if (count($this->error_handlers))
$res=end($this->error_handlers)['handler'];
else
$res=null;
if (!$this->is_callable($handler)) return null;
$this->error_handlers[]=['handler'=>$handler,'error_reporting'=>$error_reporting];
return $res;
}
/**
* Same as PHP's restore_error_handler
* @return true
*/
function restore_error_handler()
{
if (count($this->error_handlers))
array_pop($this->error_handlers);
return true;
}
/**
* The emulator error handler (in case an error happens in the emulation, that is not intended/caught/handled)
* @param [type] $errno [description]
* @param [type] $errstr [description]
* @param [type] $errfile [description]
* @param [type] $errline [description]
* @return [type] [description]
*/
function error_handler($errno, $errstr, $errfile, $errline)
{
if (count($this->error_handlers) and $errno&end($this->error_handlers)['error_reporting'])
if (false!==$this->call_function(end($this->error_handlers)['handler'],func_get_args())) return true;
$this->stash_ob();
$file=$errfile;
$line=$errline;
$file2=$line2=null;
if (isset($this->current_file)) $file2=$this->current_file;
if (isset($this->current_node)) $line2=$this->current_node->getLine();
$fatal=false;
switch($errno) //http://php.net/manual/en/errorfunc.constants.php
{
case E_USER_NOTICE:
case E_NOTICE:
$str="Notice";
break;
case E_ERROR:
case E_USER_ERROR:
$fatal=true;
$str="Error";
break;
case E_USER_WARNING:
case E_WARNING:
$str="Warning";
break;
default:
$str="Error?";
}
if ($fatal)
$fatal_str="Fatal ";
else
$fatal_str="";
$this->verbose("PHP-Emul {$str}: {$errstr} in {$file} on line {$line} ($file2:$line2)".PHP_EOL,0);
$this->output("PHP {$fatal_str}{$str}: {$errstr} in {$file2} on line {$line2}".PHP_EOL);
if ($fatal or $this->strict)
{
$this->terminated=true;
$this->termination_value=-1;
if ($this->verbose>=2)
{
$this->verbose("Emulator Backtrace:\n");
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->verbose("Emulation Backtrace:\n");
echo $this->print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
}
$this->restore_ob();
return true;
}
function todo()
{
return call_user_func_array([$this,"error"], func_get_args());
}
/**
* Used by emulator to mark emulation errors
* @param [type] $msg [description]
* @param [type] $node [description]
* @return [type] [description]
*/
public function error($msg,$node=null)
{
$this->verbose("Emulation Error: ",0);
$this->_error($msg,$node);
$this->terminated=true;
return null;
}
/**
* Core function used by all types of error (warning, error, notice, etc.)
* @param [type] $msg [description]
* @param [type] $node [description]
* @param boolean $details [description]
* @return [type] [description]
*/
protected function _error($msg,$node=null,$details=true)
{
#TODO: these should be handled by error_handler as well, and the program.
$this->verbose($msg." in ".$this->current_file." on line ".$this->current_line.PHP_EOL,0);
// $this->output($msg." in ".$this->current_file." on line ".$this->current_line.PHP_EOL);
if ($details)
{
print_r($node);
if ($this->verbose>=2)
{
$this->verbose("Emulator Backtrace:\n");
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$this->verbose("Emulation Backtrace:\n");
echo $this->print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
}
if ($this->strict)
{
$this->terminated=true;
$this->termination_value=-2;
}
}
/**
* Notices
* @param [type] $msg [description]
* @param [type] $node [description]
* @return [type] [description]
*/
public function notice($msg,$node=null)
{
if ($this->error_suppression) return false;
if ($this->error_reporting & E_NOTICE or (defined("E_USER_NOTICE") and $this->error_reporting & E_USER_NOTICE))
{
$this->verbose("Emulation Notice: ",0);
$this->_error($msg,$node,false or $this->strict);
return true;
}
return false;
}
/**
* Warnings
* @param [type] $msg [description]
* @param [type] $node [description]
* @return [type] [description]
*/
public function warning($msg,$node=null)
{
if ($this->error_suppression) return false;
if ($this->error_reporting & E_WARNING or (defined("E_USER_WARNING") and $this->error_reporting & E_USER_WARNING))
{
$this->verbose("Emulation Warning: ",0);
$this->_error($msg,$node);
return true;
}
return false;
}
}