Example #1
0
 public function __invoke($request, $response, $next)
 {
     $app = $next;
     $container = $app->getContainer();
     $settings = $container['settings'];
     if (isset($settings['debug']) === true && $settings['debug'] === true) {
         // Enable PrettyPageHandler with editor options
         $prettyPageHandler = new PrettyPageHandler();
         if (empty($settings['whoops.editor']) === false) {
             $prettyPageHandler->setEditor($settings['whoops.editor']);
         }
         // Enable JsonResponseHandler when request is AJAX
         $jsonResponseHandler = new JsonResponseHandler();
         $jsonResponseHandler->onlyForAjaxRequests(true);
         // Add more information to the PrettyPageHandler
         $prettyPageHandler->addDataTable('Slim Application', ['Application Class' => get_class($app), 'Script Name' => $app->environment->get('SCRIPT_NAME'), 'Request URI' => $app->environment->get('PATH_INFO') ?: '<none>']);
         $prettyPageHandler->addDataTable('Slim Application (Request)', array('Accept Charset' => $app->request->getHeader('ACCEPT_CHARSET') ?: '<none>', 'Content Charset' => $app->request->getContentCharset() ?: '<none>', 'Path' => $app->request->getUri()->getPath(), 'Query String' => $app->request->getUri()->getQuery() ?: '<none>', 'HTTP Method' => $app->request->getMethod(), 'Base URL' => (string) $app->request->getUri(), 'Scheme' => $app->request->getUri()->getScheme(), 'Port' => $app->request->getUri()->getPort(), 'Host' => $app->request->getUri()->getHost()));
         // Set Whoops to default exception handler
         $whoops = new \Whoops\Run();
         $whoops->pushHandler($prettyPageHandler);
         $whoops->pushHandler($jsonResponseHandler);
         $whoops->register();
         $container['errorHandler'] = function ($c) use($whoops) {
             return new WhoopsErrorHandler($whoops);
         };
         //
         $container['whoops'] = $whoops;
     }
     return $app($request, $response);
 }
Example #2
0
 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);
 }
 /**
  * @param DI $di
  */
 public function __construct(DI $di = null)
 {
     if (!class_exists('\\Whoops\\Run')) {
         return;
     }
     if (!$di) {
         $di = DI::getDefault();
     }
     // There's only ever going to be one error page...right?
     $di->setShared('whoops.pretty_page_handler', function () use($di) {
         return (new DebugbarHandler())->setDi($di);
     });
     // There's only ever going to be one error page...right?
     $di->setShared('whoops.json_response_handler', function () {
         $jsonHandler = new JsonResponseHandler();
         $jsonHandler->onlyForAjaxRequests(true);
         return $jsonHandler;
     });
     $di->setShared('whoops', function () use($di) {
         $run = new Run();
         $run->silenceErrorsInPaths(array('/phalcon-debugbar/'), E_ALL);
         $run->pushHandler($di['whoops.pretty_page_handler']);
         $run->pushHandler($di['whoops.json_response_handler']);
         return $run;
     });
     $di['whoops']->register();
 }
Example #4
0
 public function register(Container $container)
 {
     // Setup Whoops-based error handler
     $errors = new Errors();
     $error_page = new PrettyPageHandler();
     $error_page->setPageTitle('Crikey! There was an error...');
     $error_page->setEditor('sublime');
     $error_page->addResourcePath(GRAV_ROOT . '/system/assets');
     $error_page->addCustomCss('whoops.css');
     $json_page = new JsonResponseHandler();
     $json_page->onlyForAjaxRequests(true);
     $errors->pushHandler($error_page, 'pretty');
     $errors->pushHandler(new PlainTextHandler(), 'text');
     $errors->pushHandler($json_page, 'json');
     $logger = $container['log'];
     $errors->pushHandler(function (\Exception $exception, $inspector, $run) use($logger) {
         try {
             $logger->addCritical($exception->getMessage() . ' - Trace: ' . $exception->getTraceAsString());
         } catch (\Exception $e) {
             echo $e;
         }
     }, 'log');
     $errors->register();
     $container['errors'] = $errors;
 }
 public function __invoke($request, $response, $next)
 {
     $container = $this->app->getContainer();
     $settings = DotArray::newDotArray($container['settings']);
     if ($settings['app.debug'] === true) {
         // Enable PrettyPageHandler with editor options
         $prettyPageHandler = new PrettyPageHandler();
         if ($settings->has('whoops.editor')) {
             $prettyPageHandler->setEditor($settings['whoops.editor']);
         }
         // Enable JsonResponseHandler when request is AJAX
         $jsonResponseHandler = new JsonResponseHandler();
         $jsonResponseHandler->onlyForAjaxRequests(true);
         // Add more information to the PrettyPageHandler
         $prettyPageHandler->addDataTable('Slim Application', ['Application Class' => get_class($this->app), 'Script Name' => $this->app->environment->get('SCRIPT_NAME'), 'Request URI' => $this->app->environment->get('PATH_INFO') ?: '<none>']);
         $prettyPageHandler->addDataTable('Slim Application (Request)', ['Accept Charset' => $this->app->request->getHeader('ACCEPT_CHARSET') ?: '<none>', 'Content Charset' => $this->app->request->getContentCharset() ?: '<none>', 'Path' => $this->app->request->getUri()->getPath(), 'Query String' => $this->app->request->getUri()->getQuery() ?: '<none>', 'HTTP Method' => $this->app->request->getMethod(), 'Base URL' => (string) $this->app->request->getUri(), 'Scheme' => $this->app->request->getUri()->getScheme(), 'Port' => $this->app->request->getUri()->getPort(), 'Host' => $this->app->request->getUri()->getHost()]);
         $prettyPageHandler->addDataTable('SlimFastShake Settings', $settings->flatten());
         // Set Whoops to default exception handler
         $whoops = new \Whoops\Run();
         $whoops->pushHandler($prettyPageHandler);
         $whoops->pushHandler($jsonResponseHandler);
         if (isset($container['logger'])) {
             $logger = $container['logger'];
             $whoops->pushHandler(function ($exception, $inspector, $run) use($logger) {
                 $logger->critical('Whoops: ' . $exception->getMessage());
             });
         }
         $whoops->register();
         $container['errorHandler'] = function ($c) use($whoops) {
             return new WhoopsErrorHandler($whoops);
         };
         $container['whoops'] = $whoops;
     }
     return $next($request, $response);
 }
 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;
 }
Example #7
0
 public function resetHandlers()
 {
     $grav = Grav::instance();
     $config = $grav['config']->get('system.errors');
     $jsonRequest = $_SERVER && isset($_SERVER['HTTP_ACCEPT']) && $_SERVER['HTTP_ACCEPT'] == 'application/json';
     // Setup Whoops-based error handler
     $whoops = new \Whoops\Run();
     $verbosity = 1;
     if (isset($config['display'])) {
         if (is_int($config['display'])) {
             $verbosity = $config['display'];
         } else {
             $verbosity = $config['display'] ? 1 : 0;
         }
     }
     switch ($verbosity) {
         case 1:
             $error_page = new Whoops\Handler\PrettyPageHandler();
             $error_page->setPageTitle('Crikey! There was an error...');
             $error_page->addResourcePath(GRAV_ROOT . '/system/assets');
             $error_page->addCustomCss('whoops.css');
             $whoops->pushHandler($error_page);
             break;
         case -1:
             $whoops->pushHandler(new BareHandler());
             break;
         default:
             $whoops->pushHandler(new SimplePageHandler());
             break;
     }
     if (method_exists('Whoops\\Util\\Misc', 'isAjaxRequest')) {
         //Whoops 2.0
         if (Whoops\Util\Misc::isAjaxRequest() || $jsonRequest) {
             $whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
         }
     } elseif (function_exists('Whoops\\isAjaxRequest')) {
         //Whoops 2.0.0-alpha
         if (Whoops\isAjaxRequest() || $jsonRequest) {
             $whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
         }
     } else {
         //Whoops 1.x
         $json_page = new Whoops\Handler\JsonResponseHandler();
         $json_page->onlyForAjaxRequests(true);
     }
     if (isset($config['log']) && $config['log']) {
         $logger = $grav['log'];
         $whoops->pushHandler(function ($exception, $inspector, $run) use($logger) {
             try {
                 $logger->addCritical($exception->getMessage() . ' - Trace: ' . $exception->getTraceAsString());
             } catch (\Exception $e) {
                 echo $e;
             }
         }, 'log');
     }
     $whoops->register();
 }
Example #8
0
 /**
  * {@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'));
 }
Example #9
0
 private function boot()
 {
     if (ENVIRONMENT == 'development') {
         $run = new \Whoops\Run();
         $handler = new PrettyPageHandler();
         $handler->setPageTitle("We're all going to be fired!");
         $handler->setEditor('textmate');
         $run->pushHandler($handler);
         $handler = new JsonResponseHandler();
         $handler->onlyForAjaxRequests(true);
         $run->pushHandler($handler);
         $run->register();
     }
 }
 /**
  * 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);
 }
Example #11
0
 public function __invoke($request, $response, $next)
 {
     $container = $this->app->getContainer();
     $settings = $container['settings'];
     if (isset($settings['debug']) === true && $settings['debug'] === true) {
         // Enable PrettyPageHandler with editor options
         $prettyPageHandler = new PrettyPageHandler();
         if (empty($settings['whoops.editor']) === false) {
             $prettyPageHandler->setEditor($settings['whoops.editor']);
         }
         // Enable JsonResponseHandler when request is AJAX
         $jsonResponseHandler = new JsonResponseHandler();
         $jsonResponseHandler->onlyForAjaxRequests(true);
         // Add more information to the PrettyPageHandler
         $prettyPageHandler->addDataTable('Slim Application', ['Application Class' => get_class($this->app), 'Script Name' => $container['environment']->get('SCRIPT_NAME'), 'Request URI' => $container['environment']->get('PATH_INFO') ?: '<none>']);
         $prettyPageHandler->addDataTable('Slim Application (Request)', array('Accept Charset' => $container['request']->getHeader('ACCEPT_CHARSET') ?: '<none>', 'Content Charset' => $container['request']->getContentCharset() ?: '<none>', 'Path' => $container['request']->getUri()->getPath(), 'Query String' => $container['request']->getUri()->getQuery() ?: '<none>', 'HTTP Method' => $container['request']->getMethod(), 'Base URL' => (string) $container['request']->getUri(), 'Scheme' => $container['request']->getUri()->getScheme(), 'Port' => $container['request']->getUri()->getPort(), 'Host' => $container['request']->getUri()->getHost()));
         // Set Whoops to default exception handler
         $whoops = new \Whoops\Run();
         $whoops->pushHandler($prettyPageHandler);
         $whoops->pushHandler($jsonResponseHandler);
         /*// Setup Monolog, for example:
           $logger = new \Monolog\Logger('Test');
           $logger->pushHandler(new \Monolog\Handler\StreamHandler("c:/xampp/php/logs/php_error_log"));*/
         // Place our custom handler in front of the others, capturing exceptions
         // and logging them, then passing the exception on to the other handlers:
         if (!empty($logger = $this->logger)) {
             $whoops->pushHandler(function ($exception, $inspector, $run) use($logger) {
                 $logger->error($exception->getMessage());
             });
         }
         $whoops->register();
         $container['errorHandler'] = function ($c) use($whoops) {
             return function ($request, $response, $exception) use($whoops) {
                 $handler = \Whoops\Run::EXCEPTION_HANDLER;
                 ob_start();
                 $whoops->{$handler}($exception);
                 $content = ob_get_clean();
                 $code = $exception instanceof HttpException ? $exception->getStatusCode() : 500;
                 return $response->withStatus($code)->withHeader('Content-type', 'text/html')->write($content);
             };
         };
         //
         $container['whoops'] = $whoops;
     }
     return $next($request, $response);
 }
 public function __invoke(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, $next)
 {
     $container = $this->app->getContainer();
     $settings = $container['settings'];
     // Enable PrettyPageHandler with editor options
     $prettyPageHandler = new PrettyPageHandler();
     // Enable JsonResponseHandler when request is AJAX
     $jsonResponseHandler = new JsonResponseHandler();
     $jsonResponseHandler->onlyForAjaxRequests(true);
     // Add more information to the PrettyPageHandler
     $prettyPageHandler->addDataTable('Slim Application', ['Application Class' => get_class($this->app), 'Script Name' => $this->app->environment->get('SCRIPT_NAME'), 'Request URI' => $this->app->environment->get('PATH_INFO') ?: '<none>']);
     $prettyPageHandler->addDataTable('Slim Application (Request)', array('Accept Charset' => $this->app->request->getHeader('ACCEPT_CHARSET') ?: '<none>', 'Content Charset' => $this->app->request->getContentCharset() ?: '<none>', 'Path' => $this->app->request->getUri()->getPath(), 'Query String' => $this->app->request->getUri()->getQuery() ?: '<none>', 'HTTP Method' => $this->app->request->getMethod(), 'Base URL' => (string) $this->app->request->getUri(), 'Scheme' => $this->app->request->getUri()->getScheme(), 'Port' => $this->app->request->getUri()->getPort(), 'Host' => $this->app->request->getUri()->getHost()));
     // Set Whoops to default exception handler
     $whoops = new \Whoops\Run();
     $whoops->pushHandler($prettyPageHandler);
     $whoops->pushHandler($jsonResponseHandler);
     if (!empty($logger = $this->logger)) {
         $whoops->pushHandler(function ($exception, $inspector, $run) use($logger) {
             $logger->error($exception->getMessage());
         });
     }
     $whoops->register();
     // Overwrite the errorHandler
     $container['errorHandler'] = function ($c) use($whoops) {
         return function ($request, $response, $exception) use($whoops) {
             if ($exception instanceof BooBoo) {
                 // Store the BooBoo error body response in a buffer
                 ob_start();
                 BooBoo::exceptionHandler($exception);
                 $buffer = ob_get_contents();
                 ob_end_clean();
                 // By creating a new response object, all the headers set by BooBoo get resynced
                 $response = new \HTTP\Response();
                 return $response->overwrite($buffer);
             } else {
                 $handler = \Whoops\Run::EXCEPTION_HANDLER;
                 ob_start();
                 $whoops->{$handler}($exception);
                 $content = ob_get_clean();
                 $code = $exception instanceof HttpException ? $exception->getStatusCode() : 500;
                 return $response->withStatus($code)->withHeader('Content-type', 'text/html')->write($content);
             }
         };
     };
     return $next($request, $response);
 }
 public function call()
 {
     $app = $this->app;
     if ($app->config('debug') === true) {
         // Switch to custom error handler by disable debug
         $app->config('debug', false);
         //
         $app->container->singleton('whoopsPrettyPageHandler', function () {
             return new PrettyPageHandler();
         });
         $app->container->singleton('whoopsJsonResponseHandler', function () {
             $handler = new JsonResponseHandler();
             $handler->onlyForAjaxRequests(true);
             return $handler;
         });
         $app->whoopsSlimInfoHandler = $app->container->protect(function () use($app) {
             try {
                 $request = $app->request();
             } catch (RuntimeException $e) {
                 return;
             }
             $current_route = $app->router()->getCurrentRoute();
             $route_details = array();
             if ($current_route !== null) {
                 $route_details = array('Route Name' => $current_route->getName() ?: '<none>', 'Route Pattern' => $current_route->getPattern() ?: '<none>', 'Route Middleware' => $current_route->getMiddleware() ?: '<none>');
             }
             $app->whoopsPrettyPageHandler->addDataTable('Slim Application', array_merge(array('Charset' => $request->headers('ACCEPT_CHARSET'), 'Locale' => $request->getContentCharset() ?: '<none>', 'Application Class' => get_class($app)), $route_details));
             $app->whoopsPrettyPageHandler->addDataTable('Slim Application (Request)', array('URI' => $request->getRootUri(), 'Request URI' => $request->getResourceUri(), 'Path' => $request->getPath(), 'Query String' => $request->params() ?: '<none>', 'HTTP Method' => $request->getMethod(), 'Script Name' => $request->getScriptName(), 'Base URL' => $request->getUrl(), 'Scheme' => $request->getScheme(), 'Port' => $request->getPort(), 'Host' => $request->getHost()));
         });
         // Open with editor if editor is set
         $whoops_editor = $app->config('whoops.editor');
         if ($whoops_editor !== null) {
             $app->whoopsPrettyPageHandler->setEditor($whoops_editor);
         }
         $app->container->singleton('whoops', function () use($app) {
             $run = new Run();
             $run->pushHandler($app->whoopsPrettyPageHandler);
             $run->pushHandler($app->whoopsJsonResponseHandler);
             $run->pushHandler($app->whoopsSlimInfoHandler);
             return $run;
         });
         $app->error(array($app->whoops, Run::EXCEPTION_HANDLER));
     }
     $this->next->call();
 }
 /**
  * @param DI $di
  */
 public function __construct(DI $di = null)
 {
     if (!$di) {
         $di = DI::getDefault();
     }
     // There's only ever going to be one error page...right?
     $di->setShared('whoops.pretty_page_handler', function () {
         return new PrettyPageHandler();
     });
     // There's only ever going to be one error page...right?
     $di->setShared('whoops.json_response_handler', function () {
         $jsonHandler = new JsonResponseHandler();
         $jsonHandler->onlyForAjaxRequests(true);
         return $jsonHandler;
     });
     // Retrieves info on the Phalcon environment and ships it off
     // to the PrettyPageHandler's data tables:
     // This works by adding a new handler to the stack that runs
     // before the error page, retrieving the shared page handler
     // instance, and working with it to add new data tables
     $phalcon_info_handler = function () use($di) {
         try {
             $request = $di['request'];
         } catch (Exception $e) {
             // This error occurred too early in the application's life
             // and the request instance is not yet available.
             return;
         }
         // Request info:
         $di['whoops.pretty_page_handler']->addDataTable('Phalcon Application (Request)', array('URI' => $request->getScheme() . '://' . $request->getServer('HTTP_HOST') . $request->getServer('REQUEST_URI'), 'Request URI' => $request->getServer('REQUEST_URI'), 'Path Info' => $request->getServer('PATH_INFO'), 'Query String' => $request->getServer('QUERY_STRING') ?: '<none>', 'HTTP Method' => $request->getMethod(), 'Script Name' => $request->getServer('SCRIPT_NAME'), 'Scheme' => $request->getScheme(), 'Port' => $request->getServer('SERVER_PORT'), 'Host' => $request->getServerName()));
     };
     $di->setShared('whoops', function () use($di, $phalcon_info_handler) {
         $run = new Run();
         $run->pushHandler($di['whoops.pretty_page_handler']);
         $run->pushHandler($phalcon_info_handler);
         $run->pushHandler($di['whoops.json_response_handler']);
         return $run;
     });
     $di['whoops']->register();
 }
Example #15
0
 public function resetHandlers()
 {
     $grav = Grav::instance();
     $config = $grav['config']->get('system.errors');
     // Setup Whoops-based error handler
     $whoops = new \Whoops\Run();
     if (isset($config['display'])) {
         if ($config['display']) {
             $error_page = new Whoops\Handler\PrettyPageHandler();
             $error_page->setPageTitle('Crikey! There was an error...');
             $error_page->addResourcePath(GRAV_ROOT . '/system/assets');
             $error_page->addCustomCss('whoops.css');
             $whoops->pushHandler($error_page);
         } else {
             $whoops->pushHandler(new SimplePageHandler());
         }
     }
     if (function_exists('Whoops\\isAjaxRequest')) {
         //Whoops 2
         if (Whoops\isAjaxRequest()) {
             $whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
         }
     } else {
         //Whoops 1
         $json_page = new Whoops\Handler\JsonResponseHandler();
         $json_page->onlyForAjaxRequests(true);
     }
     if (isset($config['log']) && $config['log']) {
         $logger = $grav['log'];
         $whoops->pushHandler(function ($exception, $inspector, $run) use($logger) {
             try {
                 $logger->addCritical($exception->getMessage() . ' - Trace: ' . $exception->getTraceAsString());
             } catch (\Exception $e) {
                 echo $e;
             }
         }, 'log');
     }
     $whoops->register();
 }
 /**
  * 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);
 }
Example #17
0
 public function init()
 {
     $whoops = new \Whoops\Run();
     $errorPage = new Whoops\Handler\PrettyPageHandler();
     $errorPage->setPageTitle('Oops! There was an error...');
     $whoops->pushHandler($errorPage);
     if (function_exists('Whoops\\isAjaxRequest')) {
         if (Whoops\isAjaxRequest()) {
             $whoops->pushHandler(new Whoops\Handler\JsonResponseHandler());
         }
     } else {
         $jsonPage = new Whoops\Handler\JsonResponseHandler();
         $jsonPage->onlyForAjaxRequests(true);
     }
     $whoops->pushHandler(function ($exception, $inspector, $run) {
         try {
             logg($exception->getMessage() . ' - Trace: ' . $exception->getTraceAsString(), 'critical');
         } catch (\Exception $e) {
             echo $e;
         }
     }, 'log');
     $whoops->register();
 }
Example #18
0
 public function after_plugins_load(\Pimple $app)
 {
     // Add Whoops handlers
     $app['whoopsPrettyPageHandler'] = function () {
         return new PrettyPageHandler();
     };
     $app['whoopsJsonResponseHandler'] = function () {
         $handler = new JsonResponseHandler();
         $handler->onlyForAjaxRequests(true);
         return $handler;
     };
     $app["whoopsZeptoInfoHandler"] = function () use($app) {
         // Check to see if there is a current route, otherwise
         // ignore because router isn't set up yet
         try {
             $current_route = $app['router']->current_route();
         } catch (\Exception $e) {
             return;
         }
         $route_details = array();
         $app['whoopsPrettyPageHandler']->setPageTitle('Shit hit the fan!');
         $app['whoopsPrettyPageHandler']->setEditor('sublime');
         if ($current_route !== null) {
             $route_details = array('Route URL' => $current_route->url(), 'Route Pattern' => $current_route->pattern());
         }
         $app["whoopsPrettyPageHandler"]->addDataTable('Zepto Application', array_merge(array('Charset' => $app['request']->headers->get('Accept-Charset'), 'Locale' => $app['request']->getPreferredLanguage()), $route_details));
         $app["whoopsPrettyPageHandler"]->addDataTable('Request Information', array('URI' => $app['request']->getUri(), 'Request URI' => $app['request']->getRequestUri(), 'Path' => $app['request']->getPathInfo(), 'Query String' => $app['request']->getQueryString(), 'HTTP Method' => $app['request']->getMethod(), 'Script Name' => $app['request']->getScriptName(), 'Base URL' => $app['request']->getBaseUrl(), 'Scheme' => $app['request']->getScheme(), 'Port' => $app['request']->getPort(), 'Host' => $app['request']->getHost()));
     };
     // Add actual Whoops\Run object
     $app['whoops'] = function ($app) {
         $run = new Run();
         $run->pushHandler($app['whoopsPrettyPageHandler']);
         $run->pushHandler($app['whoopsJsonResponseHandler']);
         $run->pushHandler($app['whoopsZeptoInfoHandler']);
         return $run;
     };
 }
 public function register(Container $container)
 {
     /** @var UniformResourceLocator $locator */
     $locator = $container['locator'];
     /** @var Platform $platform */
     $platform = $container['platform'];
     // Setup Whoops-based error handler
     $errors = new Run();
     $errors->registerPaths($platform->errorHandlerPaths());
     $error_page = new PrettyPageHandler();
     $error_page->setPageTitle('Crikey! There was an error...');
     $error_page->setEditor('sublime');
     foreach ($locator->findResources('gantry-assets://css/whoops.css') as $path) {
         $error_page->addResourcePath(dirname($path));
     }
     $error_page->addCustomCss('whoops.css');
     $json_page = new JsonResponseHandler();
     $json_page->onlyForAjaxRequests(true);
     $errors->pushHandler($error_page, 'pretty');
     $errors->pushHandler(new PlainTextHandler(), 'text');
     $errors->pushHandler($json_page, 'json');
     $errors->register();
     $container['errors'] = $errors;
 }
 * $ cd project_dir
 * $ php -S localhost:8080
 *
 * and access localhost:8080/examples/example-ajax-only.php through your browser
 *
 * Or just run it through apache/nginx/what-have-yous as usual.
 */
namespace Whoops\Example;

use Whoops\Run;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Handler\JsonResponseHandler;
use RuntimeException;
require __DIR__ . '/../vendor/autoload.php';
$run = new Run();
// We want the error page to be shown by default, if this is a
// regular request, so that's the first thing to go into the stack:
$run->pushHandler(new PrettyPageHandler());
// Now, we want a second handler that will run before the error page,
// and immediately return an error message in JSON format, if something
// goes awry.
$jsonHandler = new JsonResponseHandler();
// Make sure it only triggers for AJAX requests:
$jsonHandler->onlyForAjaxRequests(true);
// You can also tell JsonResponseHandler to give you a full stack trace:
// $jsonHandler->addTraceToOutput(true);
// And push it into the stack:
$run->pushHandler($jsonHandler);
// That's it! Register Whoops and throw a dummy exception:
$run->register();
throw new RuntimeException("Oh fudge napkins!");
Example #21
0
 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']);
     }
 }
 /**
  * @param JsonResponseHandler $handler
  *
  * @return void
  */
 public function prepareJsonHandler(JsonResponseHandler $handler)
 {
     $handler->addTraceToOutput(true);
 }
 /**
  * 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;
 }
Example #24
0
 /**
  * Configure handler
  * Right now there are 2 handlers: onNotFound and onError
  *
  * @return void
  */
 protected function configureHandler()
 {
     if ($this->config('_handlerConfigured') !== true) {
         $app = $this;
         if ($this->config('bono.cli') !== true) {
             $this->whoops = new Run();
             $handler = new PrettyPageHandler();
             $path = explode(DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, __DIR__);
             $path = $path[0] . '/templates/_whoops';
             $handler->setResourcesPath($path);
             $jsonResponseHandler = new JsonResponseHandler();
             $jsonResponseHandler->onlyForAjaxRequests(true);
             $appHandler = function ($err) use($app, $handler) {
                 if (!isset($app->request)) {
                     return;
                 }
                 $template = 'error.php';
                 if ($err->getMessage() === '404 Resource not found') {
                     $template = 'notFound.php';
                 }
                 $request = $app->request;
                 // Add some custom tables with relevant info about your application,
                 // that could prove useful in the error page:
                 $handler->addDataTable('Bono Application', array('Template' => 'Modify this page on templates/' . $template, 'Application Class' => get_class($app), 'Charset' => $request->headers('ACCEPT_CHARSET') ?: '<none>', 'Locale' => $request->getContentCharset() ?: '<none>'));
                 $handler->addDataTable('Bono Request', array('URI' => $request->getRootUri(), 'Request URI' => $request->getResourceUri(), 'Path' => $request->getPath(), 'Query String' => $request->params() ?: '<none>', 'HTTP Method' => $request->getMethod(), 'Script Name' => $request->getScriptName(), 'Base URL' => $request->getUrl(), 'Scheme' => $request->getScheme(), 'Port' => $request->getPort(), 'Host' => $request->getHost()));
                 // Set the title of the error page:
                 $handler->setPageTitle("Bono got whoops! There was a problem.");
             };
             $this->whoops->pushHandler($handler);
             // Add a special handler to deal with AJAX requests with an
             // equally-informative JSON response. Since this handler is
             // first in the stack, it will be executed before the error
             // page handler, and will have a chance to decide if anything
             // needs to be done.
             $this->whoops->pushHandler($jsonResponseHandler);
             $this->whoops->pushHandler($appHandler);
             $this->notFound(array(new NotFoundHandler($this), 'handle'));
             $this->error(array(new ErrorHandler($this), 'handle'));
         }
         $this->config('_handlerConfigured', true);
     }
     return $this;
 }