example-ajax-only.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. *
  6. * Run this example file with the PHP 5.4 web server with:
  7. *
  8. * $ cd project_dir
  9. * $ php -S localhost:8080
  10. *
  11. * and access localhost:8080/example/example-ajax-only.php through your browser
  12. *
  13. * Or just run it through apache/nginx/what-have-yous as usual.
  14. */
  15. namespace Whoops\Example;
  16. use Whoops\Run;
  17. use Whoops\Handler\PrettyPageHandler;
  18. use Whoops\Handler\JsonResponseHandler;
  19. use RuntimeException;
  20. require __DIR__ . '/../vendor/autoload.php';
  21. $run = new Run;
  22. // We want the error page to be shown by default, if this is a
  23. // regular request, so that's the first thing to go into the stack:
  24. $run->pushHandler(new PrettyPageHandler);
  25. // Now, we want a second handler that will run before the error page,
  26. // and immediately return an error message in JSON format, if something
  27. // goes awry.
  28. $jsonHandler = new JsonResponseHandler;
  29. // Make sure it only triggers for AJAX requests:
  30. $jsonHandler->onlyForAjaxRequests(true);
  31. // You can also tell JsonResponseHandler to give you a full stack trace:
  32. // $jsonHandler->addTraceToOutput(true);
  33. // And push it into the stack:
  34. $run->pushHandler($jsonHandler);
  35. // That's it! Register Whoops and throw a dummy exception:
  36. $run->register();
  37. throw new RuntimeException("Oh fudge napkins!");