PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/error.class.php

https://bitbucket.org/jwetmore/diablo-iii-api-tests
PHP | 252 lines | 59 code | 31 blank | 162 comment | 1 complexity | 49b9013f3f91fa93a03a5c37a3367236 MD5 | raw file
  1. <?php
  2. /**
  3. * Error Class
  4. *
  5. * This class is a helper for thrown errors in that it holds the data for an evergreen exception
  6. * to allow the error information to be accessed from url's or pages loaded by the thrown error.
  7. *
  8. *
  9. * Copyright 2007-2010, NaturalCodeProject (http://www.naturalcodeproject.com)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2007-2010, NaturalCodeProject (http://www.naturalcodeproject.com)
  15. * @package evergreen
  16. * @subpackage lib
  17. * @version $Revision$
  18. * @modifiedby $LastChangedBy$
  19. * @lastmodified $Date$
  20. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  21. */
  22. /**
  23. * Error Class
  24. *
  25. * This class is a helper for thrown errors in that it holds the data for an evergreen exception
  26. * to allow the error information to be accessed from url's or pages loaded by the thrown error.
  27. *
  28. * Hooks:
  29. * Error.setupError.before
  30. * Error.setupError.after
  31. * Error.getMessage
  32. * Error.getCode
  33. * Error.getFile
  34. * Error.getLine
  35. * Error.getTrace
  36. * Error.getParams
  37. * Error.triggered
  38. *
  39. * @package evergreen
  40. * @subpackage lib
  41. */
  42. final class Error {
  43. /**
  44. * Indicator used to tell if an error has been triggered.
  45. *
  46. * @access private
  47. * @static
  48. * @var boolean
  49. */
  50. private static $triggered = false;
  51. /**
  52. * Current triggered error's message.
  53. *
  54. * @access private
  55. * @static
  56. * @var string
  57. */
  58. private static $message = "Unknown exception";
  59. /**
  60. * Current triggered error's code.
  61. *
  62. * @access private
  63. * @static
  64. * @var mixed
  65. */
  66. private static $code = 0;
  67. /**
  68. * Current triggered error's file.
  69. *
  70. * @access private
  71. * @static
  72. * @var string
  73. */
  74. private static $file = null;
  75. /**
  76. * Current triggered error's line.
  77. *
  78. * @access private
  79. * @static
  80. * @var integer
  81. */
  82. private static $line = 0;
  83. /**
  84. * Current triggered error's trace.
  85. *
  86. * @access private
  87. * @static
  88. * @var array
  89. */
  90. private static $trace = array();
  91. /**
  92. * Current triggered error's params.
  93. *
  94. * @access private
  95. * @static
  96. * @var array
  97. */
  98. private static $params = array();
  99. /**
  100. * Sets up the error class with data from the thrown error.
  101. *
  102. * @access public
  103. * @static
  104. * @final
  105. * @param object $e The exception that was thrown
  106. */
  107. final public static function setupError($e) {
  108. // call hook
  109. Hook::call('Error.setupError.before', array(&$e));
  110. if (is_object($e)) {
  111. self::$message = $e->getMessage();
  112. self::$code = $e->getCode();
  113. self::$file = $e->getFile();
  114. self::$line = $e->getLine();
  115. self::$trace = $e->getTrace();
  116. self::$params = $e->getParams();
  117. self::$triggered = true;
  118. }
  119. // call hook
  120. Hook::call('Error.setupError.after');
  121. }
  122. /**
  123. * Returns the error message.
  124. *
  125. * @access public
  126. * @static
  127. * @final
  128. * @return string
  129. */
  130. final public static function getMessage() {
  131. $message = self::$message;
  132. // call hook
  133. Hook::call('Error.getMessage', array(&$message));
  134. return $message;
  135. }
  136. /**
  137. * Returns the error code.
  138. *
  139. * @access public
  140. * @static
  141. * @final
  142. * @return mixed
  143. */
  144. final public static function getCode() {
  145. $code = self::$code;
  146. // call hook
  147. Hook::call('Error.getCode', array(&$code));
  148. return $message;
  149. }
  150. /**
  151. * Returns the file that the error occurred in.
  152. *
  153. * @access public
  154. * @static
  155. * @final
  156. * @return string
  157. */
  158. final public static function getFile() {
  159. $file = self::$file;
  160. // call hook
  161. Hook::call('Error.getFile', array(&$file));
  162. return $file;
  163. }
  164. /**
  165. * Returns the line number the error occurred at.
  166. *
  167. * @access public
  168. * @static
  169. * @final
  170. * @return integer
  171. */
  172. final public static function getLine() {
  173. $line = self::$line;
  174. // call hook
  175. Hook::call('Error.getLine', array(&$line));
  176. return $line;
  177. }
  178. /**
  179. * Returns the error trace.
  180. *
  181. * @access public
  182. * @static
  183. * @final
  184. * @return array
  185. */
  186. final public static function getTrace() {
  187. $trace = self::$trace;
  188. // call hook
  189. Hook::call('Error.getTrace', array(&$trace));
  190. return $trace;
  191. }
  192. /**
  193. * Returns the params from the error.
  194. *
  195. * @access public
  196. * @static
  197. * @final
  198. * @return array
  199. */
  200. final public static function getParams() {
  201. $params = self::$params;
  202. // call hook
  203. Hook::call('Error.getParams', array(&$params));
  204. return $params;
  205. }
  206. /**
  207. * Returns if an error has been triggered or not.
  208. *
  209. * @access public
  210. * @static
  211. * @final
  212. * @return boolean true if an error has been triggered and boolean false if not
  213. */
  214. final public static function triggered() {
  215. $triggered = self::$triggered;
  216. // call hook
  217. Hook::call('Error.triggered', array(&$triggered));
  218. return $triggered;
  219. }
  220. }
  221. ?>