CRM add remaining missing comment blocks (autogenerated)
[civicrm-core.git] / CRM / Queue / ErrorPolicy.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * To ensure that PHP errors or unhandled exceptions are reported in JSON format,
30 * wrap this around your code. For example:
31 *
32 * @code
33 * $errorContainer = new CRM_Queue_ErrorPolicy();
34 * $errorContainer->call(function(){
35 * ...include some files, do some work, etc...
36 * });
37 * @endcode
38 *
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.
43 */
44 class CRM_Queue_ErrorPolicy {
45 var $active;
46
47 /**
48 * @param null $level
49 */
50 function __construct($level = NULL) {
51 register_shutdown_function(array($this, 'onShutdown'));
52 if ($level === NULL) {
53 $level = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
54 }
55 $this->level = $level;
56 }
57
58 function activate() {
59 $this->active = TRUE;
60 $this->backup = array();
61 foreach (array(
62 'display_errors', 'html_errors', 'xmlrpc_errors') as $key) {
63 $this->backup[$key] = ini_get($key);
64 ini_set($key, 0);
65 }
66 set_error_handler(array($this, 'onError'), $this->level);
67 // FIXME make this temporary/reversible
68 $this->errorScope = CRM_Core_TemporaryErrorScope::useException();
69 }
70
71 function deactivate() {
72 $this->errorScope = NULL;
73 restore_error_handler();
74 foreach (array(
75 'display_errors', 'html_errors', 'xmlrpc_errors') as $key) {
76 ini_set($key, $this->backup[$key]);
77 }
78 $this->active = FALSE;
79 }
80
81 /**
82 * @param $callable
83 *
84 * @return mixed
85 */
86 function call($callable) {
87 $this->activate();
88 try {
89 $result = $callable();
90 }
91 catch(Exception$e) {
92 $this->reportException($e);
93 }
94 $this->deactivate();
95 return $result;
96 }
97
98 /**
99 * Receive (semi) recoverable error notices
100 *
101 * @see set_error_handler
102 */
103 function onError($errno, $errstr, $errfile, $errline) {
104 if (!(error_reporting() & $errno)) {
105 return TRUE;
106 }
107 throw new Exception(sprintf('PHP Error %s at %s:%s: %s', $errno, $errfile, $errline, $errstr));
108 }
109
110 /**
111 * Receive non-recoverable error notices
112 *
113 * @see register_shutdown_function
114 * @see error_get_last
115 */
116 function onShutdown() {
117 if (!$this->active) {
118 return;
119 }
120 $error = error_get_last();
121 if (is_array($error) && ($error['type'] & $this->level)) {
122 $this->reportError($error);
123 }
124 }
125
126 /**
127 * Print a fatal error
128 *
129 * @param $error
130 */
131 function reportError($error) {
132 $response = array(
133 'is_error' => 1,
134 'is_continue' => 0,
135 'exception' => htmlentities(sprintf('Error %s: %s in %s, line %s', $error['type'], $error['message'], $error['file'], $error['line'])),
136 );
137 global $activeQueueRunner;
138 if (is_object($activeQueueRunner)) {
139 $response['last_task_title'] = $activeQueueRunner->lastTaskTitle;
140 }
141 CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportError', $response);
142 echo json_encode($response);
143 // civiExit() is unnecessary -- we're only called as part of abend
144 }
145
146 /**
147 * Print an unhandled exception
148 *
149 * @param $e
150 */
151 function reportException(Exception $e) {
152 CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportException', CRM_Core_Error::formatTextException($e));
153
154 $response = array(
155 'is_error' => 1,
156 'is_continue' => 0,
157 );
158
159 $config = CRM_Core_Config::singleton();
160 if ($config->backtrace || CRM_Core_Config::isUpgradeMode()) {
161 $response['exception'] = CRM_Core_Error::formatHtmlException($e);
162 }
163 else {
164 $response['exception'] = htmlentities($e->getMessage());
165 }
166
167 global $activeQueueRunner;
168 if (is_object($activeQueueRunner)) {
169 $response['last_task_title'] = $activeQueueRunner->lastTaskTitle;
170 }
171 echo json_encode($response);
172 CRM_Utils_System::civiExit();
173 }
174 }
175