PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/f/error.php

https://github.com/serafin/fine
PHP | 293 lines | 219 code | 53 blank | 21 comment | 21 complexity | 1077660127b0d678ce77f14563ca5097 MD5 | raw file
  1. <?php
  2. class f_error
  3. {
  4. const RENDER_FORMAT_HTML = 'RENDER_FORAMT_HTML';
  5. const RENDER_FORMAT_TEXT = 'RENDER_FORAMT_TEXT';
  6. public static $phpError = array(
  7. E_ERROR => 'Fatal Error',
  8. E_USER_ERROR => 'User Error',
  9. E_PARSE => 'Parse Error',
  10. E_WARNING => 'Warning',
  11. E_USER_WARNING => 'User Warning',
  12. E_STRICT => 'Strict',
  13. E_NOTICE => 'Notice',
  14. E_RECOVERABLE_ERROR => 'Recoverable Error',
  15. );
  16. /**
  17. * @var string Error or exception message
  18. */
  19. public $msg;
  20. /**
  21. * @var int Exception code, default 0
  22. */
  23. public $code;
  24. /**
  25. * @var string File
  26. */
  27. public $file;
  28. /**
  29. * @var int Line
  30. */
  31. public $line;
  32. /**
  33. * @var int|string PHP Error code or name
  34. */
  35. public $errorType;
  36. /**
  37. * @var array Error or Exception stack trace
  38. */
  39. public $trace;
  40. /**
  41. * @var Exception
  42. */
  43. public $exception;
  44. protected $_throwError;
  45. protected $_renderFormat = self::RENDER_FORMAT_HTML;
  46. public static function _()
  47. {
  48. return new self;
  49. }
  50. public function __construct(array $config = array())
  51. {
  52. foreach ($config as $k => $v) {
  53. $this->{$k}($v);
  54. }
  55. }
  56. public function register()
  57. {
  58. set_error_handler(array($this, 'handleError'));
  59. set_exception_handler(array($this, 'handleException'));
  60. }
  61. public function render($bRenderErrorAndException = null)
  62. {
  63. if ($bRenderErrorAndException === null) {
  64. return ini_get('display_errors');
  65. }
  66. ini_set('display_errors', $bRenderErrorAndException);
  67. return $this;
  68. }
  69. public function level($iErrorReportingLevel = null)
  70. {
  71. if ($iErrorReportingLevel === null) {
  72. return ini_get('error_reporting');
  73. }
  74. ini_set('error_reporting', $iErrorReportingLevel);
  75. return $this;
  76. }
  77. public function log($bLogErrors = null)
  78. {
  79. if ($bLogErrors === null) {
  80. return ini_get('log_errors');
  81. }
  82. ini_set('log_errors', $bLogErrors);
  83. return $this;
  84. }
  85. public function throwError($iLevel = null)
  86. {
  87. if ($iLevel === null) {
  88. return $this->_throwError;
  89. }
  90. $this->_throwError = $iLevel;
  91. return $this;
  92. }
  93. public function renderFormat($tRenderFormat = null)
  94. {
  95. if ($tRenderFormat === null) {
  96. return $this->_renderFormat;
  97. }
  98. $this->_renderFormat = $tRenderFormat;
  99. return $this;
  100. }
  101. public function handleError($no, $str, $file, $line)
  102. {
  103. if (! ($no & $this->level())) {
  104. return false;
  105. }
  106. if ($no & $this->_throwError) {
  107. throw new ErrorException($str, 0, $no, $file, $line);
  108. }
  109. else {
  110. $this->_clear();
  111. $this->msg = $str;
  112. $this->code = $no;
  113. $this->file = $file;
  114. $this->line = $line;
  115. $this->errorType = self::$phpError[$no];
  116. $this->trace = debug_backtrace();
  117. array_shift($this->trace); // remove trace for this fuction call
  118. $this->onError();
  119. if ($this->log()) {
  120. error_log($this->_renderErrorAsString());
  121. }
  122. if ($this->render()) {
  123. $this->_renderError();
  124. }
  125. return true;
  126. }
  127. }
  128. public function handleException($exception)
  129. {
  130. $this->_clear();
  131. try {
  132. $this->exception = $exception;
  133. $this->msg = $exception->getMessage();
  134. $this->code = $exception->getCode();
  135. $this->file = $exception->getFile();
  136. $this->line = $exception->getLine();
  137. $this->trace = $exception->getTrace();
  138. if ($this->exception instanceof ErrorException) {
  139. array_shift($this->trace); // removing handler call
  140. }
  141. $oControllerError = new c_error();
  142. $oControllerError->error();
  143. $this->onException();
  144. if ($this->log()) {
  145. error_log($this->_renderExceptionAsString());
  146. }
  147. if ($this->render()) {
  148. $this->_renderException();
  149. }
  150. }
  151. catch (Exception $e) {
  152. if ($this->log()) {
  153. error_log((string)$e);
  154. }
  155. if ($this->render()) {
  156. echo $e;
  157. }
  158. }
  159. }
  160. public function onError()
  161. {
  162. $this->_filterConfidentialData();
  163. }
  164. public function onException()
  165. {
  166. $this->_filterConfidentialData();
  167. }
  168. protected function _filterConfidentialData()
  169. {
  170. $sConnectMsg = "mysql_connect(): Access denied for user ";
  171. if (strncmp($this->msg, $sConnectMsg, strlen($sConnectMsg)) == 0) {
  172. $trace =& $this->trace;
  173. foreach ((array)$trace as $t_k => $t_v) {
  174. foreach ((array)$t_v['args'] as $k => $v) {
  175. $trace[$t_k]['args'][$k] = "***";
  176. }
  177. }
  178. }
  179. }
  180. protected function _renderException()
  181. {
  182. if ($this->_renderFormat == self::RENDER_FORMAT_TEXT) {
  183. $this->_renderExceptionAsString();
  184. return;
  185. }
  186. if (! headers_sent()) {
  187. header('Content-Type: text/html; charset=utf-8', TRUE, 500);
  188. }
  189. $oView = new f_v();
  190. $oView->exception = $this->exception;
  191. $oView->msg = $this->msg;
  192. $oView->code = $this->code;
  193. $oView->file = $this->file;
  194. $oView->line = $this->line;
  195. $oView->trace = $this->trace;
  196. $oView->error = $this;
  197. ob_end_clean();
  198. echo $oView->renderPath('./lib/f/error/exception.view');
  199. }
  200. protected function _renderError()
  201. {
  202. echo $this->_renderErrorAsString();
  203. }
  204. protected function _renderExceptionAsString()
  205. {
  206. return $this->_formatAsString(
  207. get_class($this->exception), $this->code, $this->msg,
  208. $this->file, $this->line, $this->trace
  209. );
  210. }
  211. protected function _renderErrorAsString()
  212. {
  213. return $this->_formatAsString(
  214. $this->errorType, $this->code, $this->msg,
  215. $this->file, $this->line, $this->trace
  216. );
  217. }
  218. protected function _formatAsString($type, $code, $msg, $file, $line, $trace)
  219. {
  220. $return = "\n#$type $code $msg $file:$line";
  221. foreach ($trace as $k => $v) {
  222. $return .= "\n#$k " . $v['class'] . $v['type'] . $v['function']
  223. . "(" . f_debug::dumpFunctionArgs($v['args']) .")"
  224. . " " .$v['file'] . ":" . $v['line'];
  225. ;
  226. }
  227. $return .= "\n#".(++$k)." {main}\n";
  228. return $return;
  229. }
  230. protected function _clear()
  231. {
  232. $this->msg = null;
  233. $this->code = null;
  234. $this->file = null;
  235. $this->line = null;
  236. $this->type = null;
  237. $this->error = null;
  238. $this->exception = null;
  239. }
  240. }