Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExceptionHandler.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Piwik\API\ResponseBuilder;
  11. use Piwik\Common;
  12. use Piwik\Plugin;
  13. /**
  14. * Contains Piwik's uncaught exception handler and log file formatting for exception
  15. * instances.
  16. */
  17. class ExceptionHandler
  18. {
  19. /**
  20. * The backtrace string to use when testing.
  21. *
  22. * @var string
  23. */
  24. public static $debugBacktraceForTests = null;
  25. public static function setUp()
  26. {
  27. Piwik::addAction('Log.formatFileMessage', array('\\Piwik\\ExceptionHandler', 'formatFileAndDBLogMessage'));
  28. Piwik::addAction('Log.formatDatabaseMessage', array('\\Piwik\\ExceptionHandler', 'formatFileAndDBLogMessage'));
  29. Piwik::addAction('Log.formatScreenMessage', array('\\Piwik\\ExceptionHandler', 'formatScreenMessage'));
  30. set_exception_handler(array('\\Piwik\\ExceptionHandler', 'logException'));
  31. }
  32. public static function formatFileAndDBLogMessage(&$message, $level, $tag, $datetime, $log)
  33. {
  34. if ($message instanceof \Exception) {
  35. $message = sprintf("%s(%d): %s\n%s", $message->getFile(), $message->getLine(), $message->getMessage(),
  36. self::$debugBacktraceForTests ? : $message->getTraceAsString());
  37. $message = $log->formatMessage($level, $tag, $datetime, $message);
  38. }
  39. }
  40. public static function formatScreenMessage(&$message, $level, $tag, $datetime, $log)
  41. {
  42. if ($message instanceof \Exception) {
  43. Common::sendHeader('Content-Type: text/html; charset=utf-8');
  44. $outputFormat = strtolower(Common::getRequestVar('format', 'html', 'string'));
  45. $response = new ResponseBuilder($outputFormat);
  46. $message = $response->getResponseException(new \Exception($message->getMessage()));
  47. }
  48. }
  49. public static function logException(\Exception $exception)
  50. {
  51. Log::error($exception);
  52. }
  53. }