/system/exceptions/SystemException.php

https://github.com/sajgak/cookiteasy · PHP · 102 lines · 89 code · 12 blank · 1 comment · 8 complexity · 999282a93855037c1064e2ebd1c00d34 MD5 · raw file

  1. <?
  2. namespace system\exceptions;
  3. class SystemException{
  4. private $errno;
  5. private $errstr;
  6. private $errfile;
  7. private $errline;
  8. private $base;
  9. private $error_levels = array(
  10. E_ERROR => 'Error',
  11. E_WARNING => 'Warning',
  12. E_PARSE => 'Parsing Error',
  13. E_NOTICE => 'Notice',
  14. E_CORE_ERROR => 'Core Error',
  15. E_CORE_WARNING => 'Core Warning',
  16. E_COMPILE_ERROR => 'Compile Error',
  17. E_COMPILE_WARNING => 'Compile Warning',
  18. E_USER_ERROR => 'User Error',
  19. E_USER_WARNING => 'User Warning',
  20. E_USER_NOTICE => 'User Notice',
  21. E_STRICT => 'Runtime Notice'
  22. );
  23. private $fatal_errors = array(
  24. E_ERROR,
  25. E_PARSE,
  26. E_CORE_ERROR,
  27. E_COMPILE_ERROR,
  28. E_COMPILE_WARNING,
  29. E_USER_ERROR
  30. );
  31. public function __construct($errno, $errstr, $errfile, $errline){
  32. $this->errno = $errno;
  33. $this->errstr = $errstr;
  34. $this->errfile = $errfile;
  35. $this->errline = $errline;
  36. $this->base = get_instance();
  37. if(!$this->base)
  38. $this->base = new \system\core\Manager();
  39. $this->process_error();
  40. }
  41. private function process_error(){
  42. $severity = ( ! isset($this->error_levels[$this->errno])) ? $this->errno : $this->error_levels[$this->errno];
  43. if (FALSE !== strpos($this->errfile, DIRECTORY_SEPARATOR)){
  44. $x = explode('/', $this->errfile);
  45. $filepath = $x[count($x)-3].DIRECTORY_SEPARATOR.$x[count($x)-2].DIRECTORY_SEPARATOR.end($x);
  46. }
  47. if (($this->errno & error_reporting()) == $this->errno)
  48. $this->show_php_error($severity, $this->errstr, $filepath, $this->errline);
  49. if($this->base->config('main')->log_errors)
  50. $this->log($severity, $this->errstr, $this->errfile, $this->errline);
  51. if(in_array($this->errno, $this->fatal_errors)){
  52. if (($this->errno & error_reporting()) != $this->errno)
  53. $this->show_fake_error();
  54. exit(1);
  55. }
  56. }
  57. private function show_php_error($severity, $message, $filepath, $line){
  58. echo '<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
  59. <h4>A PHP Error was encountered</h4>
  60. <p>Severity: '.$severity.'</p>
  61. <p>Message: '.$message.'</p>
  62. <p>Filename: '.$filepath.'</p>
  63. <p>Line Number: '.$line.'</p>
  64. </div>';
  65. //debug_print_backtrace();
  66. }
  67. private function show_fake_error(){
  68. echo '<div style="width:600px;margin:-10px -300px;border:1px solid #990000;text-align:center;position:absolute;top:50%;left:50%;">
  69. <h4>В данный момент наш сервис становится лучше</h4>
  70. <p>Пожалуйста, наберитесь терпения. Скоро все снова заработает.</p>
  71. </div>';
  72. }
  73. private function log($severity, $message, $filepath, $line){
  74. ob_start();
  75. debug_print_backtrace();
  76. $debug = ob_get_contents();
  77. ob_end_clean();
  78. $this->base->library('mongo')->insert('error_log', array(
  79. 'severity' => $severity,
  80. 'message' => $message,
  81. 'file' => $filepath,
  82. 'line' => $line,
  83. 'trace' => $debug,
  84. 'date' => time(),
  85. 'application' => APPATH
  86. ));
  87. }
  88. }