Source for file HarmoniException.class.php

Documentation is available at HarmoniException.class.php

  1. <?php
  2. /**
  3. * @since 9/5/07
  4. * @package harmoni.error_handler
  5. *
  6. * @copyright Copyright &copy; 2007, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: HarmoniException.class.php,v 1.4 2007/09/19 14:04:09 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * The HarmoniException adds pretty HTML formatting to the built-in exception class.
  14. *
  15. * @since 9/5/07
  16. * @package harmoni.error_handler
  17. *
  18. * @copyright Copyright &copy; 2007, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: HarmoniException.class.php,v 1.4 2007/09/19 14:04:09 adamfranco Exp $
  22. */
  23. class HarmoniException
  24. extends Exception
  25. {
  26. /**
  27. * @var string $type;
  28. * @access private
  29. * @since 9/5/07
  30. */
  31. private $type = '';
  32. /**
  33. * @var boolean $isFatal;
  34. * @access private
  35. * @since 9/5/07
  36. */
  37. private $isFatal = true;
  38. /**
  39. * Constructor
  40. *
  41. * @param string $message
  42. * @param optional integer $code
  43. * @param optional string $type
  44. * @return void
  45. * @access public
  46. * @since 9/5/07
  47. */
  48. public function __construct ($message, $code = 0, $type = '', $isFatal = true) {
  49. parent::__construct($message, $code);
  50. $this->type = $type;
  51. $this->isFatal = $isFatal;
  52. }
  53. /**
  54. * Answer the type of error (generally the package it occurred in).
  55. *
  56. * @return string
  57. * @access public
  58. * @since 9/5/07
  59. */
  60. public function getType () {
  61. return $this->type;
  62. }
  63. /**
  64. * Answer true if this exception causes a fatal error if not caught.
  65. *
  66. * @return boolean
  67. * @access public
  68. * @since 9/5/07
  69. */
  70. public function isFatal () {
  71. return $this->isFatal;
  72. }
  73. }
  74.  
  75. /**
  76. * Print an exception with HTML formatting.
  77. *
  78. * @param object Exception $exception
  79. * @return void
  80. * @access public
  81. * @since 9/5/07
  82. */
  83. function printExceptionInHtml ( Exception $exception ) {
  84. print "\n<div style='background-color: #FAA; border: 2px dotted #F00; padding: 10px;'><strong>Fatal error</strong>: Uncaught exception of type";
  85. print "\n\t<div style='padding-left: 20px; font-style: italic;'>".get_class($exception)."</div>";
  86. print "with message ";
  87. print "\n\t<div style='padding-left: 20px; font-style: italic;'>".$exception->getMessage()."</div>";
  88. print "\n\tin";
  89. print "\n\t<div style='padding-left: 20px;'>";
  90. printDebugBacktrace($exception->getTrace());
  91. print "\n\t</div>";
  92. print "\n</div>";
  93. logException($exception);
  94. }
  95.  
  96. /**
  97. * Log an exception to the logging system if possible.
  98. *
  99. * @param object Exception $exception
  100. * @return void
  101. * @access public
  102. * @since 9/5/07
  103. */
  104. function logException ( Exception $exception ) {
  105. // If we have an error in the error handler or the logging system,
  106. // don't infinitely loop trying to log the error of the error....
  107. $errorLoggingRecursion = FALSE;
  108. $backtrace = debug_backtrace();
  109. for ($i = 1; $i < count($backtrace); $i++) {
  110. if (isset($backtrace[$i]['function'])
  111. && strtolower($backtrace[$i]['function']) == 'logException')
  112. {
  113. $errorLoggingRecursion = true;
  114. break;
  115. }
  116. }
  117. if (class_exists('Services') && Services::serviceRunning("Logging") && !$errorLoggingRecursion) {
  118. $loggingManager = Services::getService("Logging");
  119. $log =$loggingManager->getLogForWriting("Harmoni");
  120. $formatType = new Type("logging", "edu.middlebury", "AgentsAndNodes",
  121. "A format in which the acting Agent[s] and the target nodes affected are specified.");
  122. $priorityType = new Type("logging", "edu.middlebury",
  123. ((method_exists($exception, "isFatal") && !$exception->isFatal())?"Error":"Fatal_Error"),
  124. "Events involving critical system errors.");
  125. $item = new AgentNodeEntryItem(((method_exists($exception, "getType"))?$exception->getType():get_class($exception)), $exception->getMessage());
  126. $item->setBacktrace($exception->getTrace());
  127. $item->addTextToBactrace("\n<div><strong>REQUEST_URI: </strong>".$_SERVER['REQUEST_URI']."</div>");
  128. if (isset($_SERVER['HTTP_REFERER']))
  129. $item->addTextToBactrace("\n<div><strong>HTTP_REFERER: </strong>".$_SERVER['HTTP_REFERER']."</div>");
  130. $item->addTextToBactrace("\n<div><strong>GET: </strong><pre>".print_r($_GET, true)."</pre></div>");
  131. $item->addTextToBactrace("\n<div><strong>POST: </strong><pre>".print_r($_POST, true)."</pre></div>");
  132. $item->addTextToBactrace("\n<div><strong>HTTP_USER_AGENT: </strong><pre>".print_r($_SERVER['HTTP_USER_AGENT'], true)."</pre></div>");
  133. $log->appendLogWithTypes($item, $formatType, $priorityType);
  134. }
  135. }
  136.  
  137. set_exception_handler("printExceptionInHtml");
  138.  
  139. ?>

Documentation generated on Wed, 19 Sep 2007 10:23:54 -0400 by phpDocumentor 1.3.0RC3