ThinkException.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP系统异常基类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Exception
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class ThinkException extends Exception
  24. {//类定义开始
  25. /**
  26. +----------------------------------------------------------
  27. * 异常类型
  28. +----------------------------------------------------------
  29. * @var string
  30. * @access private
  31. +----------------------------------------------------------
  32. */
  33. private $type;
  34. // 是否存在多余调试信息
  35. private $extra;
  36. /**
  37. +----------------------------------------------------------
  38. * 架构函数
  39. +----------------------------------------------------------
  40. * @access public
  41. +----------------------------------------------------------
  42. * @param string $message 异常信息
  43. +----------------------------------------------------------
  44. */
  45. public function __construct($message,$code=0,$extra=false)
  46. {
  47. parent::__construct($message,$code);
  48. $this->type = get_class($this);
  49. $this->extra = $extra;
  50. }
  51. /**
  52. +----------------------------------------------------------
  53. * 异常输出 所有异常处理类均通过__toString方法输出错误
  54. * 每次异常都会写入系统日志
  55. * 该方法可以被子类重载
  56. +----------------------------------------------------------
  57. * @access public
  58. +----------------------------------------------------------
  59. * @return array
  60. +----------------------------------------------------------
  61. */
  62. public function __toString()
  63. {
  64. $trace = $this->getTrace();
  65. if($this->extra)
  66. // 通过throw_exception抛出的异常要去掉多余的调试信息
  67. array_shift($trace);
  68. $this->class = $trace[0]['class'];
  69. $this->function = $trace[0]['function'];
  70. $this->file = $trace[0]['file'];
  71. $this->line = $trace[0]['line'];
  72. $file = file($this->file);
  73. $traceInfo='';
  74. $time = date("y-m-d H:i:m");
  75. foreach($trace as $t) {
  76. $traceInfo .= '['.$time.'] '.$t['file'].' ('.$t['line'].') ';
  77. $traceInfo .= $t['class'].$t['type'].$t['function'].'(';
  78. $traceInfo .= implode(', ', $t['args']);
  79. $traceInfo .=")\n";
  80. }
  81. $error['message'] = $this->message;
  82. $error['type'] = $this->type;
  83. $error['detail'] = L('_MODULE_').'['.MODULE_NAME.'] '.L('_ACTION_').'['.ACTION_NAME.']'."\n";
  84. $error['detail'] .= ($this->line-2).': '.$file[$this->line-3];
  85. $error['detail'] .= ($this->line-1).': '.$file[$this->line-2];
  86. $error['detail'] .= '<font color="#FF6600" >'.($this->line).': <b>'.$file[$this->line-1].'</b></font>';
  87. $error['detail'] .= ($this->line+1).': '.$file[$this->line];
  88. $error['detail'] .= ($this->line+2).': '.$file[$this->line+1];
  89. $error['class'] = $this->class;
  90. $error['function'] = $this->function;
  91. $error['file'] = $this->file;
  92. $error['line'] = $this->line;
  93. $error['trace'] = $traceInfo;
  94. //记录系统日志
  95. Log::Write('('.$this->type.') '.$this->message);
  96. return $error ;
  97. }
  98. }//类定义结束
  99. ?>