Style - Remove @access
[civicrm-core.git] / CRM / Queue / ErrorPolicy.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
30 * format, 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 public $active;
46
47 /**
48 * @param null|int $level
49 * PHP error level to capture (e.g. E_PARSE|E_USER_ERROR).
50 */
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;
55 }
56 $this->level = $level;
57 }
58
59 /**
60 * Enable the error policy.
61 */
62 public function activate() {
63 $this->active = TRUE;
64 $this->backup = array();
65 foreach (array(
66 'display_errors', 'html_errors', 'xmlrpc_errors') as $key) {
67 $this->backup[$key] = ini_get($key);
68 ini_set($key, 0);
69 }
70 set_error_handler(array($this, 'onError'), $this->level);
71 // FIXME make this temporary/reversible
72 $this->errorScope = CRM_Core_TemporaryErrorScope::useException();
73 }
74
75 /**
76 * Disable the error policy.
77 */
78 public function deactivate() {
79 $this->errorScope = NULL;
80 restore_error_handler();
81 foreach (array(
82 'display_errors', 'html_errors', 'xmlrpc_errors') as $key) {
83 ini_set($key, $this->backup[$key]);
84 }
85 $this->active = FALSE;
86 }
87
88 /**
89 * Execute the callable. Activate and deactivate the error policy
90 * automatically.
91 *
92 * @param callable|array|string $callable
93 * A callback function.
94 *
95 * @return mixed
96 */
97 public function call($callable) {
98 $this->activate();
99 try {
100 $result = $callable();
101 }
102 catch(Exception$e) {
103 $this->reportException($e);
104 }
105 $this->deactivate();
106 return $result;
107 }
108
109 /**
110 * Receive (semi) recoverable error notices
111 *
112 * @see set_error_handler
113 */
114 public function onError($errno, $errstr, $errfile, $errline) {
115 if (!(error_reporting() & $errno)) {
116 return TRUE;
117 }
118 throw new Exception(sprintf('PHP Error %s at %s:%s: %s', $errno, $errfile, $errline, $errstr));
119 }
120
121 /**
122 * Receive non-recoverable error notices
123 *
124 * @see register_shutdown_function
125 * @see error_get_last
126 */
127 public function onShutdown() {
128 if (!$this->active) {
129 return;
130 }
131 $error = error_get_last();
132 if (is_array($error) && ($error['type'] & $this->level)) {
133 $this->reportError($error);
134 }
135 }
136
137 /**
138 * Print a fatal error
139 *
140 * @param array $error
141 * The PHP error (with "type", "message", etc).
142 */
143 public function reportError($error) {
144 $response = array(
145 'is_error' => 1,
146 'is_continue' => 0,
147 'exception' => htmlentities(sprintf('Error %s: %s in %s, line %s', $error['type'], $error['message'], $error['file'], $error['line'])),
148 );
149 global $activeQueueRunner;
150 if (is_object($activeQueueRunner)) {
151 $response['last_task_title'] = $activeQueueRunner->lastTaskTitle;
152 }
153 CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportError', $response);
154 echo json_encode($response);
155 // civiExit() is unnecessary -- we're only called as part of abend
156 }
157
158 /**
159 * Print an unhandled exception
160 *
161 * @param Exception $e
162 * The unhandled exception.
163 */
164 public function reportException(Exception $e) {
165 CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportException', CRM_Core_Error::formatTextException($e));
166
167 $response = array(
168 'is_error' => 1,
169 'is_continue' => 0,
170 );
171
172 $config = CRM_Core_Config::singleton();
173 if ($config->backtrace || CRM_Core_Config::isUpgradeMode()) {
174 $response['exception'] = CRM_Core_Error::formatHtmlException($e);
175 }
176 else {
177 $response['exception'] = htmlentities($e->getMessage());
178 }
179
180 global $activeQueueRunner;
181 if (is_object($activeQueueRunner)) {
182 $response['last_task_title'] = $activeQueueRunner->lastTaskTitle;
183 }
184 CRM_Utils_JSON::output($response);
185 }
186 }