Error.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * A class for handling application errors.
  4. * @todo this class needs to be redesigned
  5. *
  6. * @author Ironpilot
  7. * @copyright Copywrite (c) 2011, STAPLE CODE
  8. *
  9. * This file is part of the STAPLE Framework.
  10. *
  11. * The STAPLE Framework is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by the
  13. * Free Software Foundation, either version 3 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * The STAPLE Framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. class Staple_Error implements SplSubject
  25. {
  26. const PAGE_NOT_FOUND = 404;
  27. const APPLICATION_ERROR = 500;
  28. const LOADER_ERROR = 501;
  29. const DB_ERROR = 502;
  30. const AUTH_ERROR = 503;
  31. const EMAIL_ERROR = 504;
  32. const FORM_ERROR = 505;
  33. const VALIDATION_ERROR = 506;
  34. const LINK_ERROR = 507;
  35. /**
  36. * The object observers
  37. * @var SplObjectStorage
  38. */
  39. private $_observers;
  40. /**
  41. * This is the callback for error handling.
  42. * @var SplObserver
  43. */
  44. protected $logger;
  45. /**
  46. * The last exception that was thrown by the system.
  47. * @var Exception
  48. */
  49. private $lastException;
  50. /**
  51. * The default constructor.
  52. */
  53. public function __construct()
  54. {
  55. $this->_observers = new SplObjectStorage();
  56. }
  57. /**
  58. * Set the logger Object
  59. * @return SplObserver $logger
  60. */
  61. public function getLogger()
  62. {
  63. return $this->logger;
  64. }
  65. /**
  66. * Get the Logger Object
  67. * @param SplObserver $logger
  68. */
  69. public function setLogger(SplObserver $logger)
  70. {
  71. $this->attach($logger);
  72. $this->logger = $logger;
  73. return $this;
  74. }
  75. /**
  76. * @return Exception $lastException
  77. */
  78. public function getLastException()
  79. {
  80. return $this->lastException;
  81. }
  82. /**
  83. * @param Exception $lastException
  84. */
  85. private function setLastException(Exception $lastException)
  86. {
  87. $this->lastException = $lastException;
  88. return $this;
  89. }
  90. /**
  91. *
  92. * handleError catches PHP Errors and displays an error page with the error details.
  93. * @param int $errno
  94. * @param string $errstr
  95. * @param string $errfile
  96. * @param int $errline
  97. */
  98. public static function handleError($errno, $errstr, $errfile, $errline)
  99. {
  100. //Convert Errors into exceptions.
  101. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  102. }
  103. /**
  104. *
  105. * handleException catches Exceptions and displays an error page with the details.
  106. * @todo create and implement and error controller that can display custom errors
  107. * per application.
  108. * @param Exception $ex
  109. */
  110. public function handleException(Exception $ex)
  111. {
  112. //handle the error
  113. $this->setLastException($ex);
  114. //Notify observers
  115. $this->notify();
  116. //Clear the output buffer
  117. ob_clean();
  118. //Get the Front Controller
  119. $main = Staple_Main::get();
  120. //Process the Header
  121. $main->processHeader(true);
  122. //Echo the error message
  123. echo "<div class=\"section\"><div class=\"row\"><div class=\"small-12 columns\"><h1><i class=\"fi-alert\"></i> ".$ex->getMessage()." Code: ".$ex->getCode()."</h1></div></div></div>";
  124. //Echo details if in dev mode
  125. if($main->inDevMode())
  126. {
  127. if(($p = $ex->getPrevious()) instanceof Exception)
  128. {
  129. echo "<p><b>Previous Error:</b> ".$p->getMessage." Code: ".$p->getCode()."</p>";
  130. }
  131. echo "<pre>".$ex->getTraceAsString()."</pre>";
  132. foreach ($ex->getTrace() as $traceln)
  133. {
  134. echo "<pre>";
  135. var_dump($traceln);
  136. echo "</pre>";
  137. }
  138. }
  139. //If the site uses layout, build the default layout and put the error message inside.
  140. if(Staple_Layout::layoutExists(Staple_Config::getValue('layout', 'default')))
  141. {
  142. //Grab the current buffer contents to add to the layout
  143. $buffer = ob_get_contents();
  144. ob_clean();
  145. //Create the layout object and build the layout
  146. $layout = new Staple_Layout(Staple_Config::getValue('layout', 'default'));
  147. $layout->build($buffer);
  148. }
  149. }
  150. /**
  151. * Stub function for the future addition of error controller functionality
  152. */
  153. private function dispatchErrorController()
  154. {
  155. }
  156. /* (non-PHPdoc)
  157. * @see SplSubject::attach()
  158. */
  159. public function attach(SplObserver $observer)
  160. {
  161. $this->_observers->attach($observer);
  162. }
  163. /* (non-PHPdoc)
  164. * @see SplSubject::detach()
  165. */
  166. public function detach(SplObserver $observer)
  167. {
  168. $this->_observers->detach($observer);
  169. }
  170. /* (non-PHPdoc)
  171. * @see SplSubject::notify()
  172. */
  173. public function notify()
  174. {
  175. foreach($this->_observers as $observer)
  176. {
  177. $observer->update($this);
  178. }
  179. }
  180. }