19 const EXCEPTION_HANDLER =
"handleException";
20 const ERROR_HANDLER =
"handleError";
21 const SHUTDOWN_HANDLER =
"handleShutdown";
24 protected $allowQuit =
true;
25 protected $sendOutput =
true;
30 protected $sendHttpCode = 500;
35 protected $handlerStack = array();
37 protected $silencedPatterns = array();
48 if (is_callable($handler)) {
54 "Argument to " . __METHOD__ .
" must be a callable, or instance of" 55 .
"Whoops\\Handler\\HandlerInterface" 59 $this->handlerStack[] = $handler;
70 return array_pop($this->handlerStack);
80 return $this->handlerStack;
90 $this->handlerStack = array();
107 public function register()
109 if (!$this->isRegistered) {
112 class_exists(
"\\Whoops\\Exception\\ErrorException");
113 class_exists(
"\\Whoops\\Exception\\FrameCollection");
114 class_exists(
"\\Whoops\\Exception\\Frame");
115 class_exists(
"\\Whoops\\Exception\\Inspector");
117 set_error_handler(array($this, self::ERROR_HANDLER));
118 set_exception_handler(array($this, self::EXCEPTION_HANDLER));
119 register_shutdown_function(array($this, self::SHUTDOWN_HANDLER));
121 $this->isRegistered =
true;
133 if ($this->isRegistered) {
134 restore_exception_handler();
135 restore_error_handler();
137 $this->isRegistered =
false;
150 if (func_num_args() == 0) {
151 return $this->allowQuit;
154 return $this->allowQuit = (bool) $exit;
165 $this->silencedPatterns = array_merge(
166 $this->silencedPatterns,
168 function ($pattern) use ($levels) {
170 "pattern" => $pattern,
190 if (func_num_args() == 0) {
191 return $this->sendHttpCode;
195 return $this->sendHttpCode =
false;
198 if (
$code ===
true) {
204 "Invalid status code '$code', must be 4xx or 5xx" 208 return $this->sendHttpCode =
$code;
219 if (func_num_args() == 0) {
220 return $this->sendOutput;
223 return $this->sendOutput = (bool) $send;
237 $inspector = $this->getInspector($exception);
245 $handlerResponse = null;
247 foreach (array_reverse($this->handlerStack) as $handler) {
248 $handler->setRun($this);
249 $handler->setInspector($inspector);
250 $handler->setException($exception);
256 $handlerResponse = $handler->handle($exception);
258 if (in_array($handlerResponse, array(Handler::LAST_HANDLER, Handler::QUIT))) {
267 $willQuit = $handlerResponse == Handler::QUIT && $this->allowQuit();
269 $output = ob_get_clean();
274 if ($this->writeToOutput()) {
279 while (ob_get_level() > 0) {
284 $this->writeToOutputNow($output);
311 if ($level & error_reporting()) {
312 foreach ($this->silencedPatterns as $entry) {
313 $pathMatches = (bool) preg_match($entry[
"pattern"],
$file);
314 $levelMatches = $level & $entry[
"levels"];
315 if ($pathMatches && $levelMatches) {
322 if ($this->canThrowExceptions) {
325 $this->handleException($exception);
344 $this->canThrowExceptions =
false;
346 $error = error_get_last();
347 if ($error && $this->isLevelFatal($error[
'type'])) {
363 private $canThrowExceptions =
true;
372 if ($this->sendHttpCode() && \
Whoops\Util\Misc::canSendHeaders()) {
373 $httpCode = $this->sendHttpCode();
375 if (function_exists(
'http_response_code')) {
376 http_response_code($httpCode);
387 header(
'X-Ignore-This: 1',
true, $httpCode);
clearHandlers()
Clears all handlers in the handlerStack, including the default PrettyPage handler.
handleException(Exception $exception)
Handles an exception, ultimately generating a Whoops error page.
writeToOutput($send=null)
Should Whoops push output directly to the client? If this is false, output will be returned by handle...
handleError($level, $message, $file=null, $line=null)
Converts generic PHP errors to instances, before passing them off to be handled. ...
writeToOutputNow($output)
Echo something to the browser.
getInspector(Exception $exception)
silenceErrorsInPaths($patterns, $levels=10240)
Silence particular errors in particular files.
allowQuit($exit=null)
Should Whoops allow Handlers to force the script to quit?
getHandlers()
Returns an array with all handlers, in the order they were added to the stack.
Whoops - php errors for cool kids.
unregister()
Unregisters all handlers registered by this Whoops instance.
pushHandler($handler)
Pushes a handler to the end of the stack.
handleShutdown()
Special case to deal with Fatal errors and the like.
popHandler()
Removes the last handler in the stack and returns it.
Wrapper for Closures passed as handlers.
Wraps ErrorException; mostly used for typing (at least now) to easily cleanup the stack trace of redu...
A Whoops error handler that delegates calls on it self to another handler that is created only in the...
static isLevelFatal($level)