3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * To ensure that PHP errors or unhandled exceptions are reported in JSON
30 * format, wrap this around your code. For example:
33 * $errorContainer = new CRM_Queue_ErrorPolicy();
34 * $errorContainer->call(function() {
35 * ...include some files, do some work, etc...
39 * Note: Most of the code in this class is pretty generic vis-a-vis error
40 * handling -- except for 'reportError', whose message format is only
41 * appropriate for use with the CRM_Queue_Page_AJAX. Some kind of cleanup
42 * will be necessary to get reuse from the other parts of this class.
44 class CRM_Queue_ErrorPolicy
{
48 * @param null|int $level
49 * PHP error level to capture (e.g. E_PARSE|E_USER_ERROR).
51 public function __construct($level = NULL) {
52 register_shutdown_function(array($this, 'onShutdown'));
53 if ($level === NULL) {
54 $level = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR
;
56 $this->level
= $level;
60 * Enable the error policy.
62 public function activate() {
64 $this->backup
= array();
70 $this->backup
[$key] = ini_get($key);
73 set_error_handler(array($this, 'onError'), $this->level
);
74 // FIXME make this temporary/reversible
75 $this->errorScope
= CRM_Core_TemporaryErrorScope
::useException();
79 * Disable the error policy.
81 public function deactivate() {
82 $this->errorScope
= NULL;
83 restore_error_handler();
89 ini_set($key, $this->backup
[$key]);
91 $this->active
= FALSE;
95 * Execute the callable. Activate and deactivate the error policy
98 * @param callable|array|string $callable
99 * A callback function.
103 public function call($callable) {
106 $result = $callable();
108 catch (Exception
$e) {
109 $this->reportException($e);
116 * Receive (semi) recoverable error notices
118 * @see set_error_handler
120 public function onError($errno, $errstr, $errfile, $errline) {
121 if (!(error_reporting() & $errno)) {
124 throw new Exception(sprintf('PHP Error %s at %s:%s: %s', $errno, $errfile, $errline, $errstr));
128 * Receive non-recoverable error notices
130 * @see register_shutdown_function
131 * @see error_get_last
133 public function onShutdown() {
134 if (!$this->active
) {
137 $error = error_get_last();
138 if (is_array($error) && ($error['type'] & $this->level
)) {
139 $this->reportError($error);
144 * Print a fatal error.
146 * @param array $error
147 * The PHP error (with "type", "message", etc).
149 public function reportError($error) {
153 'exception' => htmlentities(sprintf('Error %s: %s in %s, line %s', $error['type'], $error['message'], $error['file'], $error['line'])),
155 global $activeQueueRunner;
156 if (is_object($activeQueueRunner)) {
157 $response['last_task_title'] = $activeQueueRunner->lastTaskTitle
;
159 CRM_Core_Error
::debug_var('CRM_Queue_ErrorPolicy_reportError', $response);
160 echo json_encode($response);
161 // civiExit() is unnecessary -- we're only called as part of abend
165 * Print an unhandled exception.
167 * @param Exception $e
168 * The unhandled exception.
170 public function reportException(Exception
$e) {
171 CRM_Core_Error
::debug_var('CRM_Queue_ErrorPolicy_reportException', CRM_Core_Error
::formatTextException($e));
178 $config = CRM_Core_Config
::singleton();
179 if ($config->backtrace || CRM_Core_Config
::isUpgradeMode()) {
180 $response['exception'] = CRM_Core_Error
::formatHtmlException($e);
183 $response['exception'] = htmlentities($e->getMessage());
186 global $activeQueueRunner;
187 if (is_object($activeQueueRunner)) {
188 $response['last_task_title'] = $activeQueueRunner->lastTaskTitle
;
190 CRM_Utils_JSON
::output($response);