PageRenderTime 66ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/system/php/lang/Exception.php

https://gitlab.com/alvinahmadov2/hhvm
PHP | 278 lines | 147 code | 24 blank | 107 comment | 28 complexity | 755b2db0c5d71820193e42d47b97dd49 MD5 | raw file
  1. <?php
  2. // This doc comment block generated by idl/sysdoc.php
  3. /**
  4. * ( excerpt from http://docs.hhvm.com/manual/en/class.exception.php )
  5. *
  6. * Exception is the base class for all Exceptions.
  7. *
  8. */
  9. class Exception {
  10. protected $message = ''; // exception message
  11. private $string = ''; // php5 has this, we don't use it
  12. protected $code = 0; // user defined exception code
  13. protected $file; // source filename of exception
  14. protected $line; // source line of exception
  15. private $trace; // full stacktrace
  16. private $previous = null;
  17. private static $traceOpts = 0;
  18. /**
  19. * This cannot be implemented in __construct, because a derived class may
  20. * implement its own __construct, losing the stacktrace. The runtime has
  21. * special logic to call the __init__ method before
  22. * calling __construct just to make sure $this->trace is always populated.
  23. */
  24. final public function __init__() {
  25. if (isset($this->trace)) {
  26. return;
  27. }
  28. $this->initTrace();
  29. }
  30. private function __check_init($context) {
  31. if (!isset($this->trace)) {
  32. trigger_error($context.': exception object not initialized', E_WARNING);
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. }
  38. // This doc comment block generated by idl/sysdoc.php
  39. /**
  40. * ( excerpt from http://docs.hhvm.com/manual/en/exception.construct.php )
  41. *
  42. * Constructs the Exception.
  43. *
  44. * @message mixed The Exception message to throw.
  45. * @code mixed The Exception code.
  46. * @previous mixed The previous exception used for the exception
  47. * chaining.
  48. */
  49. public function __construct($message = '', $code = 0,
  50. Exception $previous = null) {
  51. // Child classes may just override the protected property
  52. // without implementing a constructor or calling parent one.
  53. // In this case we should not override it from the param.
  54. if ($message !== '' || $this->message === '') {
  55. $this->message = $message;
  56. }
  57. if ($code !== 0 || $this->code === 0) {
  58. $this->code = $code;
  59. }
  60. $this->previous = $previous;
  61. }
  62. // This doc comment block generated by idl/sysdoc.php
  63. /**
  64. * ( excerpt from http://docs.hhvm.com/manual/en/exception.getmessage.php )
  65. *
  66. * Returns the Exception message.
  67. *
  68. * @return mixed Returns the Exception message as a string.
  69. */
  70. public function getMessage() {
  71. return $this->message;
  72. }
  73. // This doc comment block generated by idl/sysdoc.php
  74. /**
  75. * ( excerpt from http://docs.hhvm.com/manual/en/exception.getprevious.php )
  76. *
  77. * Returns previous Exception (the third parameter of
  78. * Exception::__construct()).
  79. *
  80. * @return mixed Returns the previous Exception if available or NULL
  81. * otherwise.
  82. */
  83. final public function getPrevious() {
  84. return $this->previous;
  85. }
  86. final public function setPrevious(Exception $previous) {
  87. $this->previous = $previous;
  88. }
  89. final public function setPreviousChain(Exception $previous) {
  90. $cur = $this;
  91. $next = $cur->getPrevious();
  92. while ($next instanceof Exception) {
  93. $cur = $next;
  94. $next = $cur->getPrevious();
  95. }
  96. $cur->setPrevious($previous);
  97. }
  98. // This doc comment block generated by idl/sysdoc.php
  99. /**
  100. * ( excerpt from http://docs.hhvm.com/manual/en/exception.getcode.php )
  101. *
  102. * Returns the Exception code.
  103. *
  104. * @return mixed Returns the exception code as integer in Exception
  105. * but possibly as other type in Exception descendants
  106. * (for example as string in PDOException).
  107. */
  108. public function getCode() {
  109. return $this->code;
  110. }
  111. // This doc comment block generated by idl/sysdoc.php
  112. /**
  113. * ( excerpt from http://docs.hhvm.com/manual/en/exception.getfile.php )
  114. *
  115. * Get the name of the file the exception was created.
  116. *
  117. * @return mixed Returns the filename in which the exception was
  118. * created.
  119. */
  120. final public function getFile() {
  121. if (!$this->__check_init(__METHOD__)) {
  122. return null;
  123. }
  124. return $this->file;
  125. }
  126. // This doc comment block generated by idl/sysdoc.php
  127. /**
  128. * ( excerpt from http://docs.hhvm.com/manual/en/exception.getline.php )
  129. *
  130. * Get line number where the exception was created.
  131. *
  132. * @return mixed Returns the line number where the exception was
  133. * created.
  134. */
  135. final public function getLine() {
  136. if (!$this->__check_init(__METHOD__)) {
  137. return null;
  138. }
  139. return $this->line;
  140. }
  141. // This doc comment block generated by idl/sysdoc.php
  142. /**
  143. * ( excerpt from http://docs.hhvm.com/manual/en/exception.gettrace.php )
  144. *
  145. * Returns the Exception stack trace.
  146. *
  147. * @return mixed Returns the Exception stack trace as an array.
  148. */
  149. final public function getTrace() {
  150. if (!$this->__check_init(__METHOD__)) {
  151. return null;
  152. }
  153. return $this->trace;
  154. }
  155. // This doc comment block generated by idl/sysdoc.php
  156. /**
  157. * ( excerpt from http://docs.hhvm.com/manual/en/exception.gettraceasstring.php )
  158. *
  159. * Returns the Exception stack trace as a string.
  160. *
  161. * @return mixed Returns the Exception stack trace as a string.
  162. */
  163. final public function getTraceAsString() {
  164. if (!$this->__check_init(__METHOD__)) {
  165. return null;
  166. }
  167. $i = 0;
  168. $s = "";
  169. foreach ($this->getTrace() as $frame) {
  170. if (!is_array($frame)) continue;
  171. $s .= "#$i " .
  172. (isset($frame['file']) ? $frame['file'] : "") . "(" .
  173. (isset($frame['line']) ? $frame['line'] : "") . "): " .
  174. (isset($frame['class']) ? $frame['class'] . $frame['type'] : "") .
  175. $frame['function'] . "()\n";
  176. $i++;
  177. }
  178. $s .= "#$i {main}";
  179. return $s;
  180. }
  181. /* Overrideable */
  182. // formated string for display
  183. // This doc comment block generated by idl/sysdoc.php
  184. /**
  185. * ( excerpt from http://docs.hhvm.com/manual/en/exception.tostring.php )
  186. *
  187. * Returns the string representation of the exception.
  188. *
  189. * @return mixed Returns the string representation of the exception.
  190. */
  191. public function __toString() {
  192. $res = "";
  193. $lst = array();
  194. $ex = $this;
  195. while ($ex != null) {
  196. $lst[] = $ex;
  197. $ex = $ex->previous;
  198. }
  199. $lst = array_reverse($lst);
  200. foreach ($lst as $i => $ex) {
  201. if ($i > 0) {
  202. $res .= "\n\nNext ";
  203. }
  204. $res .= "exception '" . get_class($ex) . "' with message '" .
  205. $ex->getMessage() . "' in " . $ex->getFile() . ":" .
  206. $ex->getLine() . "\nStack trace:\n" . $ex->getTraceAsString();
  207. }
  208. return $res;
  209. }
  210. /**
  211. * Derived classes may override the methods below if different behavior
  212. * for initializing the trace is desired
  213. */
  214. protected function initTrace() {
  215. $this->trace = debug_backtrace(static::getTraceOptions());
  216. // Remove top stack frames up to and including Exception::__init__,
  217. // set the 'file' and 'line' properties appropriately
  218. while (!empty($this->trace)) {
  219. $top = array_shift($this->trace);
  220. if (isset($top['class']) && isset($top['function']) &&
  221. strcasecmp($top['class'], 'exception') === 0 &&
  222. strcasecmp($top['function'], '__init__') === 0) {
  223. if (isset($top['file'])) $this->file = $top['file'];
  224. if (isset($top['line'])) $this->line = $top['line'];
  225. break;
  226. }
  227. }
  228. // Remove systemlib stack frames until we hit the user code.
  229. // Assume user code will contain the elements for file and line.
  230. if (($this->file === null) && ($this->line === null)) {
  231. while (!empty($this->trace)
  232. && !isset($this->trace[0]['file']) && !isset($this->trace[0]['line'])
  233. ) {
  234. array_shift($this->trace);
  235. }
  236. if (isset($this->trace[0]['file'])) {
  237. $this->file = $this->trace[0]['file'];
  238. }
  239. if (isset($this->trace[0]['line'])) {
  240. $this->line = $this->trace[0]['line'];
  241. }
  242. }
  243. }
  244. public static function getTraceOptions() {
  245. return self::$traceOpts;
  246. }
  247. public static function setTraceOptions($opts) {
  248. self::$traceOpts = (int)$opts;
  249. }
  250. final private function __clone() {
  251. trigger_error("Trying to clone an uncloneable object of class " .
  252. get_class($this), E_USER_ERROR);
  253. }
  254. }