PEAR_Error

Standard PEAR error class for PHP 4.

Defined (1)

The class is defined in the following location(s).

/inc/pear/pear.php  
  1. class PEAR_Error 
  2. // {{{ properties 
  3.  
  4. var $error_message_prefix = ''; 
  5. var $mode = PEAR_ERROR_RETURN; 
  6. var $level = E_USER_NOTICE; 
  7. var $code = -1; 
  8. var $message = ''; 
  9. var $userinfo = ''; 
  10. var $backtrace = null; 
  11.  
  12. // }}} 
  13. // {{{ constructor 
  14.  
  15. /** 
  16. * PEAR_Error constructor 
  17. * @param string $message message 
  18. * @param int $code (optional) error code 
  19. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,  
  20. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,  
  21. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION 
  22. * @param mixed $options (optional) error level, _OR_ in the case of 
  23. * PEAR_ERROR_CALLBACK, the callback function or object/method 
  24. * tuple. 
  25. * @param string $userinfo (optional) additional user/debug info 
  26. * @access public 
  27. */ 
  28. function PEAR_Error($message = 'unknown error', $code = null,  
  29. $mode = null, $options = null, $userinfo = null) 
  30. if ($mode === null) { 
  31. $mode = PEAR_ERROR_RETURN; 
  32. $this->message = $message; 
  33. $this->code = $code; 
  34. $this->mode = $mode; 
  35. $this->userinfo = $userinfo; 
  36. if (function_exists("debug_backtrace")) { 
  37. if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) { 
  38. $this->backtrace = debug_backtrace(); 
  39. if ($mode & PEAR_ERROR_CALLBACK) { 
  40. $this->level = E_USER_NOTICE; 
  41. $this->callback = $options; 
  42. } else { 
  43. if ($options === null) { 
  44. $options = E_USER_NOTICE; 
  45. $this->level = $options; 
  46. $this->callback = null; 
  47. if ($this->mode & PEAR_ERROR_PRINT) { 
  48. if (is_null($options) || is_int($options)) { 
  49. $format = "%s"; 
  50. } else { 
  51. $format = $options; 
  52. printf($format, $this->getMessage()); 
  53. if ($this->mode & PEAR_ERROR_TRIGGER) { 
  54. trigger_error($this->getMessage(), $this->level); 
  55. if ($this->mode & PEAR_ERROR_DIE) { 
  56. $msg = $this->getMessage(); 
  57. if (is_null($options) || is_int($options)) { 
  58. $format = "%s"; 
  59. if (substr($msg, -1) != "\n") { 
  60. $msg .= "\n"; 
  61. } else { 
  62. $format = $options; 
  63. die(sprintf($format, $msg)); 
  64. if ($this->mode & PEAR_ERROR_CALLBACK) { 
  65. if (is_callable($this->callback)) { 
  66. call_user_func($this->callback, $this); 
  67. if ($this->mode & PEAR_ERROR_EXCEPTION) { 
  68. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING); 
  69. eval('$e = new Exception($this->message, $this->code);throw($e);'); 
  70.  
  71. // }}} 
  72. // {{{ getMode() 
  73.  
  74. /** 
  75. * Get the error mode from an error object. 
  76. * @return int error mode 
  77. * @access public 
  78. */ 
  79. function getMode() { 
  80. return $this->mode; 
  81.  
  82. // }}} 
  83. // {{{ getCallback() 
  84.  
  85. /** 
  86. * Get the callback function/method from an error object. 
  87. * @return mixed callback function or object/method array 
  88. * @access public 
  89. */ 
  90. function getCallback() { 
  91. return $this->callback; 
  92.  
  93. // }}} 
  94. // {{{ getMessage() 
  95.  
  96.  
  97. /** 
  98. * Get the error message from an error object. 
  99. * @return string full error message 
  100. * @access public 
  101. */ 
  102. function getMessage() 
  103. return ($this->error_message_prefix . $this->message); 
  104.  
  105.  
  106. // }}} 
  107. // {{{ getCode() 
  108.  
  109. /** 
  110. * Get error code from an error object 
  111. * @return int error code 
  112. * @access public 
  113. */ 
  114. function getCode() 
  115. return $this->code; 
  116.  
  117. // }}} 
  118. // {{{ getType() 
  119.  
  120. /** 
  121. * Get the name of this error/exception. 
  122. * @return string error/exception name (type) 
  123. * @access public 
  124. */ 
  125. function getType() 
  126. return get_class($this); 
  127.  
  128. // }}} 
  129. // {{{ getUserInfo() 
  130.  
  131. /** 
  132. * Get additional user-supplied information. 
  133. * @return string user-supplied information 
  134. * @access public 
  135. */ 
  136. function getUserInfo() 
  137. return $this->userinfo; 
  138.  
  139. // }}} 
  140. // {{{ getDebugInfo() 
  141.  
  142. /** 
  143. * Get additional debug information supplied by the application. 
  144. * @return string debug information 
  145. * @access public 
  146. */ 
  147. function getDebugInfo() 
  148. return $this->getUserInfo(); 
  149.  
  150. // }}} 
  151. // {{{ getBacktrace() 
  152.  
  153. /** 
  154. * Get the call backtrace from where the error was generated. 
  155. * Supported with PHP 4.3.0 or newer. 
  156. * @param int $frame (optional) what frame to fetch 
  157. * @return array Backtrace, or NULL if not available. 
  158. * @access public 
  159. */ 
  160. function getBacktrace($frame = null) 
  161. if (defined('PEAR_IGNORE_BACKTRACE')) { 
  162. return null; 
  163. if ($frame === null) { 
  164. return $this->backtrace; 
  165. return $this->backtrace[$frame]; 
  166.  
  167. // }}} 
  168. // {{{ addUserInfo() 
  169.  
  170. function addUserInfo($info) 
  171. if (empty($this->userinfo)) { 
  172. $this->userinfo = $info; 
  173. } else { 
  174. $this->userinfo .= " ** $info"; 
  175.  
  176. // }}} 
  177. // {{{ toString() 
  178.  
  179. /** 
  180. * Make a string representation of this object. 
  181. * @return string a string with an object summary 
  182. * @access public 
  183. */ 
  184. function toString() { 
  185. $modes = array(); 
  186. $levels = array(E_USER_NOTICE => 'notice',  
  187. E_USER_WARNING => 'warning',  
  188. E_USER_ERROR => 'error'); 
  189. if ($this->mode & PEAR_ERROR_CALLBACK) { 
  190. if (is_array($this->callback)) { 
  191. $callback = (is_object($this->callback[0]) ? 
  192. strtolower(get_class($this->callback[0])) : 
  193. $this->callback[0]) . '::' . 
  194. $this->callback[1]; 
  195. } else { 
  196. $callback = $this->callback; 
  197. return sprintf('[%s: message="%s" code=%d mode=callback '. 
  198. 'callback=%s prefix="%s" info="%s"]',  
  199. strtolower(get_class($this)), $this->message, $this->code,  
  200. $callback, $this->error_message_prefix,  
  201. $this->userinfo); 
  202. if ($this->mode & PEAR_ERROR_PRINT) { 
  203. $modes[] = 'print'; 
  204. if ($this->mode & PEAR_ERROR_TRIGGER) { 
  205. $modes[] = 'trigger'; 
  206. if ($this->mode & PEAR_ERROR_DIE) { 
  207. $modes[] = 'die'; 
  208. if ($this->mode & PEAR_ERROR_RETURN) { 
  209. $modes[] = 'return'; 
  210. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. 
  211. 'prefix="%s" info="%s"]',  
  212. strtolower(get_class($this)), $this->message, $this->code,  
  213. implode("|", $modes), $levels[$this->level],  
  214. $this->error_message_prefix,  
  215. $this->userinfo); 
  216.  
  217. // }}}