Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
4523a2f5 TO |
13 | * To ensure that PHP errors or unhandled exceptions are reported in JSON |
14 | * format, wrap this around your code. For example: | |
6a488035 | 15 | * |
0b882a86 | 16 | * ``` |
6a488035 | 17 | * $errorContainer = new CRM_Queue_ErrorPolicy(); |
9b873358 | 18 | * $errorContainer->call(function() { |
6a488035 TO |
19 | * ...include some files, do some work, etc... |
20 | * }); | |
0b882a86 | 21 | * ``` |
6a488035 TO |
22 | * |
23 | * Note: Most of the code in this class is pretty generic vis-a-vis error | |
24 | * handling -- except for 'reportError', whose message format is only | |
25 | * appropriate for use with the CRM_Queue_Page_AJAX. Some kind of cleanup | |
26 | * will be necessary to get reuse from the other parts of this class. | |
27 | */ | |
28 | class CRM_Queue_ErrorPolicy { | |
4523a2f5 | 29 | public $active; |
e0ef6999 EM |
30 | |
31 | /** | |
4523a2f5 TO |
32 | * @param null|int $level |
33 | * PHP error level to capture (e.g. E_PARSE|E_USER_ERROR). | |
e0ef6999 | 34 | */ |
4523a2f5 | 35 | public function __construct($level = NULL) { |
be2fb01f | 36 | register_shutdown_function([$this, 'onShutdown']); |
6a488035 TO |
37 | if ($level === NULL) { |
38 | $level = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR; | |
39 | } | |
40 | $this->level = $level; | |
41 | } | |
42 | ||
4523a2f5 TO |
43 | /** |
44 | * Enable the error policy. | |
45 | */ | |
46 | public function activate() { | |
6a488035 | 47 | $this->active = TRUE; |
be2fb01f CW |
48 | $this->backup = []; |
49 | foreach ([ | |
c86d4e7c SL |
50 | 'display_errors', |
51 | 'html_errors', | |
52 | 'xmlrpc_errors', | |
53 | ] as $key) { | |
6a488035 TO |
54 | $this->backup[$key] = ini_get($key); |
55 | ini_set($key, 0); | |
56 | } | |
be2fb01f | 57 | set_error_handler([$this, 'onError'], $this->level); |
6a488035 | 58 | // FIXME make this temporary/reversible |
6a488035 TO |
59 | } |
60 | ||
4523a2f5 TO |
61 | /** |
62 | * Disable the error policy. | |
63 | */ | |
64 | public function deactivate() { | |
6a488035 TO |
65 | $this->errorScope = NULL; |
66 | restore_error_handler(); | |
be2fb01f | 67 | foreach ([ |
c86d4e7c SL |
68 | 'display_errors', |
69 | 'html_errors', | |
70 | 'xmlrpc_errors', | |
71 | ] as $key) { | |
6a488035 TO |
72 | ini_set($key, $this->backup[$key]); |
73 | } | |
74 | $this->active = FALSE; | |
75 | } | |
76 | ||
e0ef6999 | 77 | /** |
4523a2f5 TO |
78 | * Execute the callable. Activate and deactivate the error policy |
79 | * automatically. | |
80 | * | |
81 | * @param callable|array|string $callable | |
82 | * A callback function. | |
e0ef6999 EM |
83 | * |
84 | * @return mixed | |
85 | */ | |
4523a2f5 | 86 | public function call($callable) { |
6a488035 TO |
87 | $this->activate(); |
88 | try { | |
89 | $result = $callable(); | |
90 | } | |
353ffa53 | 91 | catch (Exception$e) { |
6a488035 TO |
92 | $this->reportException($e); |
93 | } | |
94 | $this->deactivate(); | |
95 | return $result; | |
96 | } | |
97 | ||
98 | /** | |
ad37ac8e | 99 | * Receive (semi) recoverable error notices. |
6a488035 TO |
100 | * |
101 | * @see set_error_handler | |
ad37ac8e | 102 | * |
103 | * @param string $errno | |
104 | * @param string $errstr | |
105 | * @param string $errfile | |
106 | * @param int $errline | |
107 | * | |
108 | * @return bool | |
109 | * @throws \Exception | |
6a488035 | 110 | */ |
4523a2f5 | 111 | public function onError($errno, $errstr, $errfile, $errline) { |
6a488035 TO |
112 | if (!(error_reporting() & $errno)) { |
113 | return TRUE; | |
114 | } | |
115 | throw new Exception(sprintf('PHP Error %s at %s:%s: %s', $errno, $errfile, $errline, $errstr)); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Receive non-recoverable error notices | |
120 | * | |
121 | * @see register_shutdown_function | |
122 | * @see error_get_last | |
123 | */ | |
4523a2f5 | 124 | public function onShutdown() { |
6a488035 TO |
125 | if (!$this->active) { |
126 | return; | |
127 | } | |
128 | $error = error_get_last(); | |
129 | if (is_array($error) && ($error['type'] & $this->level)) { | |
130 | $this->reportError($error); | |
131 | } | |
132 | } | |
133 | ||
134 | /** | |
fe482240 | 135 | * Print a fatal error. |
6a488035 | 136 | * |
4523a2f5 TO |
137 | * @param array $error |
138 | * The PHP error (with "type", "message", etc). | |
6a488035 | 139 | */ |
4523a2f5 | 140 | public function reportError($error) { |
be2fb01f | 141 | $response = [ |
6a488035 TO |
142 | 'is_error' => 1, |
143 | 'is_continue' => 0, | |
144 | 'exception' => htmlentities(sprintf('Error %s: %s in %s, line %s', $error['type'], $error['message'], $error['file'], $error['line'])), | |
be2fb01f | 145 | ]; |
6a488035 TO |
146 | global $activeQueueRunner; |
147 | if (is_object($activeQueueRunner)) { | |
148 | $response['last_task_title'] = $activeQueueRunner->lastTaskTitle; | |
149 | } | |
150 | CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportError', $response); | |
151 | echo json_encode($response); | |
152 | // civiExit() is unnecessary -- we're only called as part of abend | |
153 | } | |
154 | ||
155 | /** | |
fe482240 | 156 | * Print an unhandled exception. |
6a488035 | 157 | * |
4523a2f5 TO |
158 | * @param Exception $e |
159 | * The unhandled exception. | |
6a488035 | 160 | */ |
4523a2f5 | 161 | public function reportException(Exception $e) { |
6a488035 TO |
162 | CRM_Core_Error::debug_var('CRM_Queue_ErrorPolicy_reportException', CRM_Core_Error::formatTextException($e)); |
163 | ||
be2fb01f | 164 | $response = [ |
6a488035 TO |
165 | 'is_error' => 1, |
166 | 'is_continue' => 0, | |
be2fb01f | 167 | ]; |
6a488035 TO |
168 | |
169 | $config = CRM_Core_Config::singleton(); | |
170 | if ($config->backtrace || CRM_Core_Config::isUpgradeMode()) { | |
171 | $response['exception'] = CRM_Core_Error::formatHtmlException($e); | |
172 | } | |
173 | else { | |
174 | $response['exception'] = htmlentities($e->getMessage()); | |
175 | } | |
176 | ||
177 | global $activeQueueRunner; | |
178 | if (is_object($activeQueueRunner)) { | |
179 | $response['last_task_title'] = $activeQueueRunner->lastTaskTitle; | |
180 | } | |
ecdef330 | 181 | CRM_Utils_JSON::output($response); |
6a488035 | 182 | } |
96025800 | 183 | |
6a488035 | 184 | } |