ErrorHandler.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /*
  3. * This file is part of Raven.
  4. *
  5. * (c) Sentry Team
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Event handlers for exceptions and errors
  12. *
  13. * $client = new Raven_Client('http://public:secret/example.com/1');
  14. * $error_handler = new Raven_ErrorHandler($client);
  15. * $error_handler->registerExceptionHandler();
  16. * $error_handler->registerErrorHandler();
  17. * $error_handler->registerShutdownFunction();
  18. *
  19. * @package raven
  20. */
  21. // TODO(dcramer): deprecate default error types in favor of runtime configuration
  22. // unless a reason can be determined that making them dynamic is better. They
  23. // currently are not used outside of the fatal handler.
  24. class Raven_ErrorHandler
  25. {
  26. protected $old_exception_handler;
  27. protected $call_existing_exception_handler = false;
  28. protected $old_error_handler;
  29. protected $call_existing_error_handler = false;
  30. protected $reservedMemory;
  31. /** @var Raven_Client */
  32. protected $client;
  33. protected $send_errors_last = false;
  34. protected $fatal_error_types = array(
  35. E_ERROR,
  36. E_PARSE,
  37. E_CORE_ERROR,
  38. E_CORE_WARNING,
  39. E_COMPILE_ERROR,
  40. E_COMPILE_WARNING,
  41. E_STRICT,
  42. );
  43. /**
  44. * @var array
  45. * Error types which should be processed by the handler.
  46. * A 'null' value implies "whatever error_reporting is at time of error".
  47. */
  48. protected $error_types = null;
  49. public function __construct($client, $send_errors_last = false, $error_types = null,
  50. $__error_types = null)
  51. {
  52. // support legacy fourth argument for error types
  53. if ($error_types === null) {
  54. $error_types = $__error_types;
  55. }
  56. $this->client = $client;
  57. $this->error_types = $error_types;
  58. $this->fatal_error_types = array_reduce($this->fatal_error_types, array($this, 'bitwiseOr'));
  59. if ($send_errors_last) {
  60. $this->send_errors_last = true;
  61. $this->client->store_errors_for_bulk_send = true;
  62. }
  63. }
  64. public function bitwiseOr($a, $b)
  65. {
  66. return $a | $b;
  67. }
  68. public function handleException($e, $isError = false, $vars = null)
  69. {
  70. $e->event_id = $this->client->captureException($e, null, null, $vars);
  71. if (!$isError && $this->call_existing_exception_handler) {
  72. if ($this->old_exception_handler !== null) {
  73. call_user_func($this->old_exception_handler, $e);
  74. } else {
  75. throw $e;
  76. }
  77. }
  78. }
  79. public function handleError($type, $message, $file = '', $line = 0, $context = array())
  80. {
  81. // http://php.net/set_error_handler
  82. // The following error types cannot be handled with a user defined function: E_ERROR,
  83. // E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and
  84. // most of E_STRICT raised in the file where set_error_handler() is called.
  85. if (error_reporting() !== 0) {
  86. $error_types = $this->error_types;
  87. if ($error_types === null) {
  88. $error_types = error_reporting();
  89. }
  90. if ($error_types & $type) {
  91. $e = new ErrorException($message, 0, $type, $file, $line);
  92. $this->handleException($e, true, $context);
  93. }
  94. }
  95. if ($this->call_existing_error_handler) {
  96. if ($this->old_error_handler !== null) {
  97. return call_user_func(
  98. $this->old_error_handler,
  99. $type,
  100. $message,
  101. $file,
  102. $line,
  103. $context
  104. );
  105. } else {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. public function handleFatalError()
  112. {
  113. unset($this->reservedMemory);
  114. if (null === $error = error_get_last()) {
  115. return;
  116. }
  117. if ($this->shouldCaptureFatalError($error['type'])) {
  118. $e = new ErrorException(
  119. @$error['message'], 0, @$error['type'],
  120. @$error['file'], @$error['line']
  121. );
  122. $this->handleException($e, true);
  123. }
  124. }
  125. public function shouldCaptureFatalError($type)
  126. {
  127. // Do not capture E_ERROR since those can be caught by userland since PHP 7.0
  128. // E_ERROR should already be handled by the exception handler
  129. // This prevents duplicated exceptions in PHP 7.0+
  130. if (PHP_VERSION_ID >= 70000 && $type === E_ERROR) {
  131. return false;
  132. }
  133. return $type & $this->fatal_error_types;
  134. }
  135. /**
  136. * Register a handler which will intercept unhandled exceptions and report them to the
  137. * associated Sentry client.
  138. *
  139. * @param bool $call_existing Call any existing exception handlers after processing
  140. * this instance.
  141. * @return Raven_ErrorHandler
  142. */
  143. public function registerExceptionHandler($call_existing = true)
  144. {
  145. $this->old_exception_handler = set_exception_handler(array($this, 'handleException'));
  146. $this->call_existing_exception_handler = $call_existing;
  147. return $this;
  148. }
  149. /**
  150. * Register a handler which will intercept standard PHP errors and report them to the
  151. * associated Sentry client.
  152. *
  153. * @param bool $call_existing Call any existing errors handlers after processing
  154. * this instance.
  155. * @param array $error_types All error types that should be sent.
  156. * @return Raven_ErrorHandler
  157. */
  158. public function registerErrorHandler($call_existing = true, $error_types = null)
  159. {
  160. if ($error_types !== null) {
  161. $this->error_types = $error_types;
  162. }
  163. $this->old_error_handler = set_error_handler(array($this, 'handleError'), E_ALL);
  164. $this->call_existing_error_handler = $call_existing;
  165. return $this;
  166. }
  167. /**
  168. * Register a fatal error handler, which will attempt to capture errors which
  169. * shutdown the PHP process. These are commonly things like OOM or timeouts.
  170. *
  171. * @param int $reservedMemorySize Number of kilobytes memory space to reserve,
  172. * which is utilized when handling fatal errors.
  173. * @return Raven_ErrorHandler
  174. */
  175. public function registerShutdownFunction($reservedMemorySize = 10)
  176. {
  177. register_shutdown_function(array($this, 'handleFatalError'));
  178. $this->reservedMemory = str_repeat('x', 1024 * $reservedMemorySize);
  179. return $this;
  180. }
  181. }