-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorHandler.php
120 lines (100 loc) · 3.61 KB
/
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
<?php
/**
* ErrorHandler derives from yii\web\ErrorHandler but can be customized
* in terms of which error types to handle.
*
* @author cronfy <[email protected]>
*/
namespace cronfy\yii\web;
use yii\base\ErrorException;
use yii\web\ErrorHandler as BaseErrorHandler;
class ErrorHandler extends BaseErrorHandler {
/**
* @var int php error types that will be handled by ErrorHandler.
* It is posible configuration when particular error type is discarded:
* i is handled, but not sent to log or conerted to exception.
* */
public $typesToHandle = E_ALL | E_STRICT;
/**
* @var int php error types that will be forwarded to log
* */
public $typesToLog = E_ALL | E_STRICT;
/**
* @var int php error types that will be converted to exceptions
* */
public $typesToExceptions = E_ALL | E_STRICT;
/**
* @var bool whether to catch fatal errors with ErrorHandler
*/
public $catchFatals = true;
/**
* @var boolean Used to override display_errors php ini setting
*/
public $display_errors;
/**
* @var string Used to reserve memory for fatal error handler.
*/
private $_memoryReserve;
/**
* @var \Exception from HHVM error that stores backtrace
*/
private $_hhvmException;
/**
* Register this error handler
*/
public function register()
{
if (!is_null($this->display_errors)) ini_set('display_errors', $this->display_errors);
set_exception_handler([$this, 'handleException']);
if (defined('HHVM_VERSION')) {
set_error_handler([$this, 'handleHhvmError']);
} else {
set_error_handler([$this, 'handleError']);
}
if ($this->memoryReserveSize > 0) {
$this->_memoryReserve = str_repeat('x', $this->memoryReserveSize);
}
if ($this->catchFatals) {
register_shutdown_function([$this, 'handleFatalError']);
}
}
public function handleError($code, $message, $file, $line)
{
if (!(error_reporting() & $code)) {
return false; // to internal php handler
}
if (!($code & $this->typesToHandle)) {
return false; // to internal php handler
}
if (!($code & ($this->typesToExceptions | $this->typesToLog))) {
return true; // skip internal php handler
}
// load ErrorException manually here because autoloading them will not work
// when error occurs while autoloading a class
if (!class_exists('yii\\base\\ErrorException', false)) {
// find location of yii\\base\\ErrorException
$basedir = dirname((new \ReflectionObject($this))->getParentClass()->getParentClass()->getFileName());
require_once($basedir . '/ErrorException.php');
}
$exception = new ErrorException($message, $code, $code, $file, $line);
if ($code & $this->typesToExceptions) {
// in case error appeared in __toString method we can't throw any exception
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace);
foreach ($trace as $frame) {
if ($frame['function'] === '__toString') {
$this->handleException($exception);
if (defined('HHVM_VERSION')) {
flush();
}
exit(1);
}
}
throw $exception;
}
if ($code & $this->typesToLog) {
$this->logException($exception);
}
return true; // skip internal php handler
}
}