×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: bejoy balan
Added: Aug 21, 2017 5:28 AM
Modified: Aug 21, 2017 5:32 AM
Views: 3
https://github.com/getsentry/sentry
https://github.com/getsentry/magento-amg-sentry-extension
http://www.oss.io/p/getsentry/magento-amg-sentry-extension
  1. raven-php/lib/Raven/ErrorHandler.php
  2.  
  3. <?php
  4. /*
  5.  * This file is part of Raven.
  6.  *
  7.  * (c) Sentry Team
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. /**
  13.  * Event handlers for exceptions and errors
  14.  *
  15.  * $client = new Raven_Client('http://public:secret/example.com/1');
  16.  * $error_handler = new Raven_ErrorHandler($client);
  17.  * $error_handler->registerExceptionHandler();
  18.  * $error_handler->registerErrorHandler();
  19.  * $error_handler->registerShutdownFunction();
  20.  *
  21.  * @package raven
  22.  */
  23. // TODO(dcramer): deprecate default error types in favor of runtime configuration
  24. // unless a reason can be determined that making them dynamic is better. They
  25. // currently are not used outside of the fatal handler.
  26. class Raven_ErrorHandler
  27. {
  28.     private $old_exception_handler;
  29.     private $call_existing_exception_handler = false;
  30.     private $old_error_handler;
  31.     private $call_existing_error_handler = false;
  32.     private $reservedMemory;
  33.     private $send_errors_last = false;
  34.     /**
  35.      * @var array
  36.      * Error types which should be processed by the handler.
  37.      * A 'null' value implies "whatever error_reporting is at time of error".
  38.      */
  39.     private $error_types = null;
  40.     /**
  41.      * @deprecated
  42.      * @var array
  43.      * Error types that can be processed by the handler
  44.      */
  45.     private $validErrorTypes = array(
  46.         E_ERROR,
  47.         E_WARNING,
  48.         E_PARSE,
  49.         E_NOTICE,
  50.         E_CORE_ERROR,
  51.         E_CORE_WARNING,
  52.         E_COMPILE_ERROR,
  53.         E_COMPILE_WARNING,
  54.         E_USER_ERROR,
  55.         E_USER_WARNING,
  56.         E_USER_NOTICE,
  57.         E_STRICT,
  58.         E_RECOVERABLE_ERROR,
  59.         E_DEPRECATED,
  60.         E_USER_DEPRECATED,
  61.     );
  62.     /**
  63.      * @deprecated
  64.      * @var array
  65.      * The default Error types that are always processed by the handler. Can be set during construction.
  66.      */
  67.     private $defaultErrorTypes = array(
  68.         E_ERROR,
  69.         E_PARSE,
  70.         E_CORE_ERROR,
  71.         E_CORE_WARNING,
  72.         E_COMPILE_ERROR,
  73.         E_COMPILE_WARNING,
  74.         E_STRICT,
  75.     );
  76.     public function __construct($client, $send_errors_last = false, $default_error_types = null,
  77.                                 $error_types = null)
  78.     {
  79.         $this->client = $client;
  80.         if ($default_error_types !== null) {
  81.             $this->defaultErrorTypes = $default_error_types;
  82.         }
  83.         $this->error_types = $error_types;
  84.         register_shutdown_function(array($this, 'detectShutdown'));
  85.         if ($send_errors_last) {
  86.             $this->send_errors_last = true;
  87.             $this->client->store_errors_for_bulk_send = true;
  88.             register_shutdown_function(array($this->client, 'sendUnsentErrors'));
  89.         }
  90.     }
  91.     public function handleException($e, $isError = false, $vars = null)
  92.     {
  93.         $e->event_id = $this->client->getIdent($this->client->captureException($e, null, null, $vars));
  94.         if (!$isError && $this->call_existing_exception_handler && $this->old_exception_handler) {
  95.             call_user_func($this->old_exception_handler, $e);
  96.         }
  97.     }
  98.     public function handleError($code, $message, $file = '', $line = 0, $context=array())
  99.     {
  100.         if (error_reporting() !== 0) {
  101.             $error_types = $this->error_types;
  102.             if ($error_types === null) {
  103.                 $error_types = error_reporting();
  104.             }
  105.             if ($error_types & $code) {
  106.                 $e = new ErrorException($message, 0, $code, $file, $line);
  107.                 $this->handleException($e, true, $context);
  108.             }
  109.         }
  110.         if ($this->call_existing_error_handler) {
  111.             if ($this->old_error_handler !== null) {
  112.                 return call_user_func($this->old_error_handler, $code, $message, $file, $line, $context);
  113.             } else {
  114.                 return false;
  115.             }
  116.         }
  117.     }
  118.     /**
  119.      * Nothing by default, use it in child classes for catching other types of errors
  120.      * Only constants from $this->validErrorTypes can be used
  121.      *
  122.      * @deprecated
  123.      * @return array
  124.      */
  125.     protected function getAdditionalErrorTypesToProcess()
  126.     {
  127.         return array();
  128.     }
  129.     /**
  130.      * @deprecated
  131.      * @return array
  132.      */
  133.     private function getErrorTypesToProcess()
  134.     {
  135.         $additionalErrorTypes = array_intersect($this->getAdditionalErrorTypesToProcess(), $this->validErrorTypes);
  136.         // array_unique so bitwise "or" operation wouldn't fail if some error type gets repeated
  137.         return array_unique(array_merge($this->defaultErrorTypes, $additionalErrorTypes));
  138.     }
  139.     public function handleFatalError()
  140.     {
  141.         if (null === $lastError = error_get_last()) {
  142.             return;
  143.         }
  144.         unset($this->reservedMemory);
  145.         $errors = 0;
  146.         foreach ($this->getErrorTypesToProcess() as $errorType) {
  147.             $errors |= $errorType;
  148.         }
  149.         if ($lastError['type'] & $errors) {
  150.             $e = new ErrorException(
  151.                 @$lastError['message'], @$lastError['type'], @$lastError['type'],
  152.                 @$lastError['file'], @$lastError['line']
  153.             );
  154.             $this->handleException($e, true);
  155.         }
  156.     }
  157.     public function registerExceptionHandler($call_existing_exception_handler = true)
  158.     {
  159.         $this->old_exception_handler = set_exception_handler(array($this, 'handleException'));
  160.         $this->call_existing_exception_handler = $call_existing_exception_handler;
  161.     }
  162.     /**
  163.      * Register a handler which will intercept standard PHP errors and report them to the
  164.      * associated Sentry client.
  165.      *
  166.      * @return array
  167.      */
  168.     //
  169.     public function registerErrorHandler($call_existing_error_handler = true, $error_types = null)
  170.     {
  171.         if ($error_types !== null) {
  172.             $this->error_types = $error_types;
  173.         }
  174.         $this->old_error_handler = set_error_handler(array($this, 'handleError'), E_ALL);
  175.         $this->call_existing_error_handler = $call_existing_error_handler;
  176.     }
  177.     public function registerShutdownFunction($reservedMemorySize = 10)
  178.     {
  179.         register_shutdown_function(array($this, 'handleFatalError'));
  180.         $this->reservedMemory = str_repeat('x', 1024 * $reservedMemorySize);
  181.     }
  182.     public function detectShutdown()
  183.     {
  184.         if (!defined('RAVEN_CLIENT_END_REACHED')) {
  185.             define('RAVEN_CLIENT_END_REACHED', true);
  186.         }
  187.     }
  188. }
  189. ---------------------------------------------
  190.