1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
<?php /** * Whoops - php errors for cool kids * @author Filipe Dobreira <http://github.com/filp> */
namespace Whoops;
use InvalidArgumentException; use Whoops\Exception\ErrorException; use Whoops\Exception\Inspector; use Whoops\Handler\CallbackHandler; use Whoops\Handler\Handler; use Whoops\Handler\HandlerInterface; use Whoops\Util\Misc;
final class Run { const EXCEPTION_HANDLER = "handleException"; const ERROR_HANDLER = "handleError"; const SHUTDOWN_HANDLER = "handleShutdown";
protected $isRegistered; protected $allowQuit = true; protected $sendOutput = true;
/** * @var integer|false */ protected $sendHttpCode = 500;
/** * @var HandlerInterface[] */ protected $handlerStack = array();
protected $silencedPatterns = array();
/** * Pushes a handler to the end of the stack * * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface * @param Callable|HandlerInterface $handler * @return Run */ public function pushHandler($handler) { if (is_callable($handler)) { $handler = new CallbackHandler($handler); }
if (!$handler instanceof HandlerInterface) { throw new InvalidArgumentException( "Argument to " . __METHOD__ . " must be a callable, or instance of " . "Whoops\\Handler\\HandlerInterface" ); }
$this->handlerStack[] = $handler; return $this; }
/** * Removes the last handler in the stack and returns it. * Returns null if there"s nothing else to pop. * @return null|HandlerInterface */ public function popHandler() { return array_pop($this->handlerStack); }
/** * Returns an array with all handlers, in the * order they were added to the stack. * @return array */ public function getHandlers() { return $this->handlerStack; }
/** * Clears all handlers in the handlerStack, including * the default PrettyPage handler. * @return Run */ public function clearHandlers() { $this->handlerStack = array(); return $this; }
/** * @param \Throwable $exception * @return Inspector */ protected function getInspector($exception) { return new Inspector($exception); }
/** * Registers this instance as an error handler. * @return Run */ public function register() { if (!$this->isRegistered) { // Workaround PHP bug 42098 // https://bugs.php.net/bug.php?id=42098 class_exists("\\Whoops\\Exception\\ErrorException"); class_exists("\\Whoops\\Exception\\FrameCollection"); class_exists("\\Whoops\\Exception\\Frame"); class_exists("\\Whoops\\Exception\\Inspector");
set_error_handler(array($this, self::ERROR_HANDLER)); set_exception_handler(array($this, self::EXCEPTION_HANDLER)); register_shutdown_function(array($this, self::SHUTDOWN_HANDLER));
$this->isRegistered = true; }
return $this; }
/** * Unregisters all handlers registered by this Whoops\Run instance * @return Run */ public function unregister() { if ($this->isRegistered) { restore_exception_handler(); restore_error_handler();
$this->isRegistered = false; }
return $this; }
/** * Should Whoops allow Handlers to force the script to quit? * @param bool|int $exit * @return bool */ public function allowQuit($exit = null) { if (func_num_args() == 0) { return $this->allowQuit; }
return $this->allowQuit = (bool) $exit; }
/** * Silence particular errors in particular files * @param array|string $patterns List or a single regex pattern to match * @param int $levels Defaults to E_STRICT | E_DEPRECATED * @return \Whoops\Run */ public function silenceErrorsInPaths($patterns, $levels = 10240) { $this->silencedPatterns = array_merge( $this->silencedPatterns, array_map( function ($pattern) use ($levels) { return array( "pattern" => $pattern, "levels" => $levels, ); }, (array) $patterns ) ); return $this; }
/* * Should Whoops send HTTP error code to the browser if possible? * Whoops will by default send HTTP code 500, but you may wish to * use 502, 503, or another 5xx family code. * * @param bool|int $code * @return int|false */ public function sendHttpCode($code = null) { if (func_num_args() == 0) { return $this->sendHttpCode; }
if (!$code) { return $this->sendHttpCode = false; }
if ($code === true) { $code = 500; }
if ($code < 400 || 600 <= $code) { throw new InvalidArgumentException( "Invalid status code '$code', must be 4xx or 5xx" ); }
return $this->sendHttpCode = $code; }
/** * Should Whoops push output directly to the client? * If this is false, output will be returned by handleException * @param bool|int $send * @return bool */ public function writeToOutput($send = null) { if (func_num_args() == 0) { return $this->sendOutput; }
return $this->sendOutput = (bool) $send; }
/** * Handles an exception, ultimately generating a Whoops error * page. * * @param \Throwable $exception * @return string Output generated by handlers */ public function handleException($exception) { // Walk the registered handlers in the reverse order // they were registered, and pass off the exception $inspector = $this->getInspector($exception);
// Capture output produced while handling the exception, // we might want to send it straight away to the client, // or return it silently. ob_start();
// Just in case there are no handlers: $handlerResponse = null;
foreach (array_reverse($this->handlerStack) as $handler) { $handler->setRun($this); $handler->setInspector($inspector); $handler->setException($exception);
// The HandlerInterface does not require an Exception passed to handle() // and neither of our bundled handlers use it. // However, 3rd party handlers may have already relied on this parameter, // and removing it would be possibly breaking for users. $handlerResponse = $handler->handle($exception);
if (in_array($handlerResponse, array(Handler::LAST_HANDLER, Handler::QUIT))) { // The Handler has handled the exception in some way, and // wishes to quit execution (Handler::QUIT), or skip any // other handlers (Handler::LAST_HANDLER). If $this->allowQuit // is false, Handler::QUIT behaves like Handler::LAST_HANDLER break; } }
$willQuit = $handlerResponse == Handler::QUIT && $this->allowQuit();
$output = ob_get_clean();
// If we're allowed to, send output generated by handlers directly // to the output, otherwise, and if the script doesn't quit, return // it so that it may be used by the caller if ($this->writeToOutput()) { // @todo Might be able to clean this up a bit better // If we're going to quit execution, cleanup all other output // buffers before sending our own output: if ($willQuit) { while (ob_get_level() > 0) { ob_end_clean(); } }
$this->writeToOutputNow($output); }
if ($willQuit) { flush(); // HHVM fix for https://github.com/facebook/hhvm/issues/4055 exit(1); }
return $output; }
/** * Converts generic PHP errors to \ErrorException * instances, before passing them off to be handled. * * This method MUST be compatible with set_error_handler. * * @param int $level * @param string $message * @param string $file * @param int $line * * @return bool * @throws ErrorException */ public function handleError($level, $message, $file = null, $line = null) { if ($level & error_reporting()) { foreach ($this->silencedPatterns as $entry) { $pathMatches = (bool) preg_match($entry["pattern"], $file); $levelMatches = $level & $entry["levels"]; if ($pathMatches && $levelMatches) { // Ignore the error, abort handling return true; } }
// XXX we pass $level for the "code" param only for BC reasons. // see https://github.com/filp/whoops/issues/267 $exception = new ErrorException($message, /*code*/ $level, /*severity*/ $level, $file, $line); if ($this->canThrowExceptions) { throw $exception; } else { $this->handleException($exception); } // Do not propagate errors which were already handled by Whoops. return true; }
// Propagate error to the next handler, allows error_get_last() to // work on silenced errors. return false; }
/** * Special case to deal with Fatal errors and the like. */ public function handleShutdown() { // If we reached this step, we are in shutdown handler. // An exception thrown in a shutdown handler will not be propagated // to the exception handler. Pass that information along. $this->canThrowExceptions = false;
$error = error_get_last(); if ($error && Misc::isLevelFatal($error['type'])) { // If there was a fatal error, // it was not handled in handleError yet. $this->handleError( $error['type'], $error['message'], $error['file'], $error['line'] ); } }
/** * In certain scenarios, like in shutdown handler, we can not throw exceptions * @var bool */ private $canThrowExceptions = true;
/** * Echo something to the browser * @param string $output * @return $this */ private function writeToOutputNow($output) { if ($this->sendHttpCode() && \Whoops\Util\Misc::canSendHeaders()) { $httpCode = $this->sendHttpCode();
if (function_exists('http_response_code')) { http_response_code($httpCode); } else { // http_response_code is added in 5.4. // For compatibility with 5.3 we use the third argument in header call // First argument must be a real header. // If it is empty, PHP will ignore the third argument. // If it is invalid, such as a single space, Apache will handle it well, // but the PHP development server will hang. // Setting a full status line would require us to hardcode // string values for all different status code, and detect the protocol. // which is an extra error-prone complexity. header('X-Ignore-This: 1', true, $httpCode); } }
echo $output;
return $this; } }
|