2121 * @package raven
2222 */
2323
24+ // TODO(dcramer): deprecate default error types in favor of runtime configuration
25+ // unless a reason can be determined that making them dynamic is better. They
26+ // currently are not used outside of the fatal handler.
2427class Raven_ErrorHandler
2528{
2629 private $ old_exception_handler ;
@@ -29,11 +32,60 @@ class Raven_ErrorHandler
2932 private $ call_existing_error_handler = false ;
3033 private $ reservedMemory ;
3134 private $ send_errors_last = false ;
32- private $ error_types = -1 ;
3335
34- public function __construct ($ client , $ send_errors_last = false )
36+ /**
37+ * @var array
38+ * Error types which should be processed by the handler.
39+ * A 'null' value implies "whatever error_reporting is at time of error".
40+ */
41+ private $ error_types = null ;
42+
43+ /**
44+ * @deprecated
45+ * @var array
46+ * Error types that can be processed by the handler
47+ */
48+ private $ validErrorTypes = array (
49+ E_ERROR ,
50+ E_WARNING ,
51+ E_PARSE ,
52+ E_NOTICE ,
53+ E_CORE_ERROR ,
54+ E_CORE_WARNING ,
55+ E_COMPILE_ERROR ,
56+ E_COMPILE_WARNING ,
57+ E_USER_ERROR ,
58+ E_USER_WARNING ,
59+ E_USER_NOTICE ,
60+ E_STRICT ,
61+ E_RECOVERABLE_ERROR ,
62+ E_DEPRECATED ,
63+ E_USER_DEPRECATED ,
64+ );
65+
66+ /**
67+ * @deprecated
68+ * @var array
69+ * The default Error types that are always processed by the handler. Can be set during construction.
70+ */
71+ private $ defaultErrorTypes = array (
72+ E_ERROR ,
73+ E_PARSE ,
74+ E_CORE_ERROR ,
75+ E_CORE_WARNING ,
76+ E_COMPILE_ERROR ,
77+ E_COMPILE_WARNING ,
78+ E_STRICT ,
79+ );
80+
81+ public function __construct ($ client , $ send_errors_last = false , $ default_error_types = null ,
82+ $ error_types = null )
3583 {
3684 $ this ->client = $ client ;
85+ if ($ default_error_types !== null ) {
86+ $ this ->defaultErrorTypes = $ default_error_types ;
87+ }
88+ $ this ->error_types = $ error_types ;
3789 register_shutdown_function (array ($ this , 'detectShutdown ' ));
3890 if ($ send_errors_last ) {
3991 $ this ->send_errors_last = true ;
@@ -53,16 +105,49 @@ public function handleException($e, $isError = false, $vars = null)
53105
54106 public function handleError ($ code , $ message , $ file = '' , $ line = 0 , $ context =array ())
55107 {
56- if ($ this ->error_types & $ code & error_reporting ()) {
57- $ e = new ErrorException ($ message , 0 , $ code , $ file , $ line );
58- $ this ->handleException ($ e , true , $ context );
108+ if (error_reporting () !== 0 ) {
109+ $ error_types = $ this ->error_types ;
110+ if ($ error_types === null ) {
111+ $ error_types = error_reporting ();
112+ }
113+ if ($ error_types & $ code ) {
114+ $ e = new ErrorException ($ message , 0 , $ code , $ file , $ line );
115+ $ this ->handleException ($ e , true , $ context );
116+ }
59117 }
60-
61- if ($ this ->call_existing_error_handler && $ this ->old_error_handler ) {
62- call_user_func ($ this ->old_error_handler , $ code , $ message , $ file , $ line , $ context );
118+ if ($ this ->call_existing_error_handler ) {
119+ if ($ this ->old_error_handler !== null ) {
120+ return call_user_func ($ this ->old_error_handler , $ code , $ message , $ file , $ line , $ context );
121+ } else {
122+ return false ;
123+ }
63124 }
64125 }
65126
127+ /**
128+ * Nothing by default, use it in child classes for catching other types of errors
129+ * Only constants from $this->validErrorTypes can be used
130+ *
131+ * @deprecated
132+ * @return array
133+ */
134+
135+ protected function getAdditionalErrorTypesToProcess ()
136+ {
137+ return array ();
138+ }
139+
140+ /**
141+ * @deprecated
142+ * @return array
143+ */
144+ private function getErrorTypesToProcess ()
145+ {
146+ $ additionalErrorTypes = array_intersect ($ this ->getAdditionalErrorTypesToProcess (), $ this ->validErrorTypes );
147+ // array_unique so bitwise "or" operation wouldn't fail if some error type gets repeated
148+ return array_unique (array_merge ($ this ->defaultErrorTypes , $ additionalErrorTypes ));
149+ }
150+
66151 public function handleFatalError ()
67152 {
68153 if (null === $ lastError = error_get_last ()) {
@@ -71,7 +156,10 @@ public function handleFatalError()
71156
72157 unset($ this ->reservedMemory );
73158
74- $ errors = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_STRICT ;
159+ $ errors = 0 ;
160+ foreach ($ this ->getErrorTypesToProcess () as $ errorType ) {
161+ $ errors |= $ errorType ;
162+ }
75163
76164 if ($ lastError ['type ' ] & $ errors ) {
77165 $ e = new ErrorException (
@@ -88,10 +176,19 @@ public function registerExceptionHandler($call_existing_exception_handler = true
88176 $ this ->call_existing_exception_handler = $ call_existing_exception_handler ;
89177 }
90178
91- public function registerErrorHandler ($ call_existing_error_handler = true , $ error_types = -1 )
179+ /**
180+ * Register a handler which will intercept standard PHP errors and report them to the
181+ * associated Sentry client.
182+ *
183+ * @return array
184+ */
185+ //
186+ public function registerErrorHandler ($ call_existing_error_handler = true , $ error_types = null )
92187 {
93- $ this ->error_types = $ error_types ;
94- $ this ->old_error_handler = set_error_handler (array ($ this , 'handleError ' ), error_reporting ());
188+ if ($ error_types !== null ) {
189+ $ this ->error_types = $ error_types ;
190+ }
191+ $ this ->old_error_handler = set_error_handler (array ($ this , 'handleError ' ), E_ALL );
95192 $ this ->call_existing_error_handler = $ call_existing_error_handler ;
96193 }
97194
@@ -102,7 +199,8 @@ public function registerShutdownFunction($reservedMemorySize = 10)
102199 $ this ->reservedMemory = str_repeat ('x ' , 1024 * $ reservedMemorySize );
103200 }
104201
105- public function detectShutdown () {
202+ public function detectShutdown ()
203+ {
106204 if (!defined ('RAVEN_CLIENT_END_REACHED ' )) {
107205 define ('RAVEN_CLIENT_END_REACHED ' , true );
108206 }
0 commit comments