private function attachListeners(EventInterface $event) { $request = $event->getRequest(); $application = $event->getApplication(); $services = $application->getServiceManager(); $events = $application->getEventManager(); $config = $services->get('Config'); //Display exceptions based on configuration and console mode if ($request instanceof ConsoleRequest || empty($config['view_manager']['display_exceptions'])) { return; } $jsonHandler = new JsonResponseHandler(); if (!empty($config['view_manager']['json_exceptions']['show_trace'])) { //Add trace to the JSON output $jsonHandler->addTraceToOutput(true); } if (!empty($config['view_manager']['json_exceptions']['ajax_only'])) { //Only return JSON response for AJAX requests $jsonHandler->onlyForAjaxRequests(true); } if (!empty($config['view_manager']['json_exceptions']['display'])) { //Turn on JSON handler $this->run->pushHandler($jsonHandler); } //Attach the Whoops ExceptionStrategy $exceptionStrategy = new ExceptionStrategy($this->run); $exceptionStrategy->attach($events); //Attach the Whoops RouteNotFoundStrategy $routeNotFoundStrategy = new RouteNotFoundStrategy($this->run); $routeNotFoundStrategy->attach($events); //Detach default ExceptionStrategy $services->get('Zend\\Mvc\\View\\Http\\ExceptionStrategy')->detach($events); //Detach default RouteNotFoundStrategy $services->get('Zend\\Mvc\\View\\Http\\RouteNotFoundStrategy')->detach($events); }
private static function getWhoopsInstance(ServerRequestInterface $request) { $whoops = new Run(); if (php_sapi_name() === 'cli') { $whoops->pushHandler(new PlainTextHandler()); return $whoops; } $format = FormatNegotiator::getPreferredFormat($request); switch ($format) { case 'json': $handler = new JsonResponseHandler(); $handler->addTraceToOutput(true); break; case 'html': $handler = new PrettyPageHandler(); break; case 'txt': $handler = new PlainTextHandler(); $handler->addTraceToOutput(true); break; case 'xml': $handler = new XmlResponseHandler(); $handler->addTraceToOutput(true); break; default: if (empty($format)) { $handler = new PrettyPageHandler(); } else { $handler = new PlainTextHandler(); $handler->addTraceToOutput(true); } } $whoops->pushHandler($handler); return $whoops; }
/** * If configuration indicates a JsonResponseHandler, configure and register it. * * @param Whoops $whoops * @param array|\ArrayAccess $config */ private function registerJsonHandler(Whoops $whoops, $config) { if (!isset($config['json_exceptions']['display']) || empty($config['json_exceptions']['display'])) { return; } $handler = new JsonResponseHandler(); if (isset($config['json_exceptions']['show_trace'])) { $handler->addTraceToOutput(true); } if (isset($config['json_exceptions']['ajax_only'])) { $handler->onlyForAjaxRequests(true); } $whoops->pushHandler($handler); }
/** * {@inheritDoc} */ public function onBootstrap(EventInterface $e) { $config = $e->getTarget()->getServiceManager()->get('Config'); $config = isset($config['view_manager']) ? $config['view_manager'] : array(); if ($e->getRequest() instanceof ConsoleRequest || empty($config['display_exceptions'])) { return; } $this->run = new Run(); $this->run->register(); // set up whoops config $prettyPageHandler = new PrettyPageHandler(); if (isset($config['editor'])) { if ($config['editor'] == 'phpStorm') { $localPath = null; if (isset($config['local_path'])) { $localPath = $config['local_path']; } $prettyPageHandler->setEditor(function ($file, $line) use($localPath) { if ($localPath) { // if your development server is not local it's good to map remote files to local $translations = array('^' . __DIR__ => $config['editor_path']); // change to your path foreach ($translations as $from => $to) { $file = preg_replace('#' . $from . '#', $to, $file, 1); } } return "pstorm://{$file}:{$line}"; }); } else { $prettyPageHandler->setEditor($config['editor']); } } if (!empty($config['json_exceptions']['display'])) { $jsonHandler = new JsonResponseHandler(); if (!empty($config['json_exceptions']['show_trace'])) { $jsonHandler->addTraceToOutput(true); } if (!empty($config['json_exceptions']['ajax_only'])) { $jsonHandler->onlyForAjaxRequests(true); } $this->run->pushHandler($jsonHandler); } if (!empty($config['whoops_no_catch'])) { $this->noCatchExceptions = $config['whoops_no_catch']; } $this->run->pushHandler($prettyPageHandler); $eventManager = $e->getTarget()->getEventManager(); $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'prepareException')); $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'prepareException')); }
/** * If configuration indicates a JsonResponseHandler, configure and register it. * * @param Whoops $whoops * @param array|\ArrayAccess $config */ private function registerJsonHandler(Whoops $whoops, $config) { if (!isset($config['json_exceptions']['display']) || empty($config['json_exceptions']['display'])) { return; } $handler = new JsonResponseHandler(); if (isset($config['json_exceptions']['show_trace'])) { $handler->addTraceToOutput(true); } if (isset($config['json_exceptions']['ajax_only'])) { if (method_exists(\Whoops\Util\Misc::class, 'isAjaxRequest')) { // Whoops 2.x if (!\Whoops\Util\Misc::isAjaxRequest()) { return; } } elseif (method_exists($handler, 'onlyForAjaxRequests')) { // Whoops 1.x $handler->onlyForAjaxRequests(true); } } $whoops->pushHandler($handler); }
/** * Initializes the error handler. * @return ErrorHandler The initializes error handler */ private function initializeErrorHandler() : ErrorHandler { $errorHandler = new ErrorHandler(); $errorHandler->allowQuit(false); $errorHandler->writeToOutput(false); if ($this->errorLog !== null) { $logger = new PlainTextHandler($this->getErrorLogger()); $logger->loggerOnly(true); $errorHandler->pushHandler($logger); } if ($this->debug) { if ($this->jsonRequest) { $responseHandler = new JsonResponseHandler(); $responseHandler->addTraceToOutput(true); } else { $responseHandler = new PrettyPageHandler(); } $errorHandler->pushHandler($responseHandler); } return $errorHandler; }
/** * @param JsonResponseHandler $handler * * @return void */ public function prepareJsonHandler(JsonResponseHandler $handler) { $handler->addTraceToOutput(true); }
protected function jsonResponseInit(JsonResponseHandler $handler) { $options = $this->whoopsConfig['handler']['options']; if (!empty($options['showTrace'])) { $handler->addTraceToOutput($options['showTrace']); } if (!empty($options['ajaxOnly'])) { $handler->onlyForAjaxRequests($options['ajaxOnly']); } }