ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
在base.php文件中,用一句代码\\think\\Error::register();实现错误和异常处理机制的注册。 // 注册错误和异常处理机制 ~~~ \think\Error::register(); ~~~ thinkphp\library\think\Error.php ``` namespace think; use think\console\Output as ConsoleOutput; use think\exception\ErrorException; use think\exception\Handle; use think\exception\ThrowableError; class Error { /** * 注册异常处理 * @access public * @return void */ public static function register() { error_reporting(E_ALL); /* 指定appError来处理系统错误,捕获错误后把错误以异常的形式抛出。 当程序出现错误的时候自动调用appError函数, 该函数实例化了一个PHP自带的错误异常类ErrorException, 如果符合异常处理的,就将错误信息以异常的形式抛出来,否则将错误信息写入日志中 */ set_error_handler([__CLASS__, 'appError']); /* 指定appException来处理用户抛出的异常, 如果不是异常,就实例化ThrowableError类,将异常包装起来并抛出 */ set_exception_handler([__CLASS__, 'appException']); /* 指定appShutdown处理超时异常。 注册一个会在php中止时执行的函数 注册一个callback,它会在脚本执行完成或者exit()后被调用。 */ register_shutdown_function([__CLASS__, 'appShutdown']); } /** * 异常处理 * @access public * @param \Exception|\Throwable $e 异常 * @return void */ public static function appException($e) { if (!$e instanceof \Exception) { $e = new ThrowableError($e); } //获取handle类默认handle类,你也可以在配置件文件的exception_handle项设置自己的handle类 $handler = self::getExceptionHandler(); $handler->report($e); //将错误记录到日志 if (IS_CLI) { $handler->renderForConsole(new ConsoleOutput, $e);//输出错误日志到cli窗口 } else { $handler->render($e)->send();//输出错误日志到页面 } } /** * 错误处理 * @access public * @param integer $errno 错误编号 * @param integer $errstr 详细错误信息 * @param string $errfile 出错的文件 * @param integer $errline 出错行号 * @return void * @throws ErrorException */ public static function appError($errno, $errstr, $errfile = '', $errline = 0) { $exception = new ErrorException($errno, $errstr, $errfile, $errline); // 符合异常处理的则将错误信息托管至 think\exception\ErrorException if (error_reporting() & $errno) { throw $exception; } self::getExceptionHandler()->report($exception); } /** * 异常中止处理 * @access public * @return void */ public static function appShutdown() { // 将错误信息托管至 think\ErrorException if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) { self::appException(new ErrorException( $error['type'], $error['message'], $error['file'], $error['line'] )); } // 写入日志 Log::save(); } /** * 确定错误类型是否致命 * @access protected * @param int $type 错误类型 * @return bool */ protected static function isFatal($type) { return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]); } /** * 获取异常处理的实例 * @access public * @return Handle */ public static function getExceptionHandler() { static $handle; if (!$handle) { // 异常处理 handle $class = Config::get('exception_handle'); if ($class && is_string($class) && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle") ) { $handle = new $class; } else { $handle = new Handle; if ($class instanceof \Closure) { $handle->setRender($class); } } } return $handle; } } ``` thinkphp\library\think\exception\Handle.php ``` namespace think\exception; use Exception; use think\App; use think\Config; use think\console\Output; use think\Lang; use think\Log; use think\Response; class Handle { protected $render; protected $ignoreReport = [ '\\think\\exception\\HttpException', ]; public function setRender($render) { $this->render = $render; } /** * Report or log an exception. * * @param \Exception $exception * @return void */ public function report(Exception $exception) { if (!$this->isIgnoreReport($exception)) { // 收集异常数据 if (App::$debug) { $data = [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $this->getMessage($exception), 'code' => $this->getCode($exception), ]; $log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]"; } else { $data = [ 'code' => $this->getCode($exception), 'message' => $this->getMessage($exception), ]; $log = "[{$data['code']}]{$data['message']}"; } if (Config::get('record_trace')) { $log .= "\r\n" . $exception->getTraceAsString(); } Log::record($log, 'error'); } } protected function isIgnoreReport(Exception $exception) { foreach ($this->ignoreReport as $class) { if ($exception instanceof $class) { return true; } } return false; } /** * Render an exception into an HTTP response. * * @param \Exception $e * @return Response */ public function render(Exception $e) { if ($this->render && $this->render instanceof \Closure) { $result = call_user_func_array($this->render, [$e]); if ($result) { return $result; } } if ($e instanceof HttpException) { return $this->renderHttpException($e); } else { return $this->convertExceptionToResponse($e); } } /** * @param Output $output * @param Exception $e */ public function renderForConsole(Output $output, Exception $e) { if (App::$debug) { $output->setVerbosity(Output::VERBOSITY_DEBUG); } $output->renderException($e); } /** * @param HttpException $e * @return Response */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); $template = Config::get('http_exception_template'); if (!App::$debug && !empty($template[$status])) { return Response::create($template[$status], 'view', $status)->assign(['e' => $e]); } else { return $this->convertExceptionToResponse($e); } } /** * @param Exception $exception * @return Response */ protected function convertExceptionToResponse(Exception $exception) { // 收集异常数据 if (App::$debug) { // 调试模式,获取详细的错误信息 $data = [ 'name' => get_class($exception), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $this->getMessage($exception), 'trace' => $exception->getTrace(), 'code' => $this->getCode($exception), 'source' => $this->getSourceCode($exception), 'datas' => $this->getExtendData($exception), 'tables' => [ 'GET Data' => $_GET, 'POST Data' => $_POST, 'Files' => $_FILES, 'Cookies' => $_COOKIE, 'Session' => isset($_SESSION) ? $_SESSION : [], 'Server/Request Data' => $_SERVER, 'Environment Variables' => $_ENV, 'ThinkPHP Constants' => $this->getConst(), ], ]; } else { // 部署模式仅显示 Code 和 Message $data = [ 'code' => $this->getCode($exception), 'message' => $this->getMessage($exception), ]; if (!Config::get('show_error_msg')) { // 不显示详细错误信息 $data['message'] = Config::get('error_message'); } } //保留一层 while (ob_get_level() > 1) { ob_end_clean(); } $data['echo'] = ob_get_clean(); ob_start(); extract($data); include Config::get('exception_tmpl'); // 获取并清空缓存 $content = ob_get_clean(); $response = new Response($content, 'html'); if ($exception instanceof HttpException) { $statusCode = $exception->getStatusCode(); $response->header($exception->getHeaders()); } if (!isset($statusCode)) { $statusCode = 500; } $response->code($statusCode); return $response; } /** * 获取错误编码 * ErrorException则使用错误级别作为错误编码 * @param \Exception $exception * @return integer 错误编码 */ protected function getCode(Exception $exception) { $code = $exception->getCode(); if (!$code && $exception instanceof ErrorException) { $code = $exception->getSeverity(); } return $code; } /** * 获取错误信息 * ErrorException则使用错误级别作为错误编码 * @param \Exception $exception * @return string 错误信息 */ protected function getMessage(Exception $exception) { $message = $exception->getMessage(); if (IS_CLI) { return $message; } if (strpos($message, ':')) { $name = strstr($message, ':', true); $message = Lang::has($name) ? Lang::get($name) . strstr($message, ':') : $message; } elseif (strpos($message, ',')) { $name = strstr($message, ',', true); $message = Lang::has($name) ? Lang::get($name) . ':' . substr(strstr($message, ','), 1) : $message; } elseif (Lang::has($message)) { $message = Lang::get($message); } return $message; } /** * 获取出错文件内容 * 获取错误的前9行和后9行 * @param \Exception $exception * @return array 错误文件内容 */ protected function getSourceCode(Exception $exception) { // 读取前9行和后9行 $line = $exception->getLine(); $first = ($line - 9 > 0) ? $line - 9 : 1; try { $contents = file($exception->getFile()); $source = [ 'first' => $first, 'source' => array_slice($contents, $first - 1, 19), ]; } catch (Exception $e) { $source = []; } return $source; } /** * 获取异常扩展信息 * 用于非调试模式html返回类型显示 * @param \Exception $exception * @return array 异常类定义的扩展数据 */ protected function getExtendData(Exception $exception) { $data = []; if ($exception instanceof \think\Exception) { $data = $exception->getData(); } return $data; } /** * 获取常量列表 * @return array 常量列表 */ private static function getConst() { return get_defined_constants(true)['user']; } } ``` # **重写Handle类** 由上可知错误将会由appShutdown、appError与appException接管, 并由Handle或者自定义Handle类report选择组装并输出错误 name我们试着自定义错误处理类重写render 首先修改`exception_handle` ~~~ 'exception_handle'=>'app\lib\exception\ExceptionHandle' ~~~ 然后新建application/lib/exception/ExceptionHandler.php 负责渲染错误信息 ~~~ namespace app\lib\exception; use think\Exception; use think\exception\Handle; use think\Request; /** * 注意: * tp默认调用的异常处理类是think\exception\Handle; * 调用异常处理类可以在config.php配置默认为空 'exception_handle'=>'',调用的是 think\exception\Handle * 默认调用application/lib/exception/下的这个类则需要修改配置为:'exception_handle'=>'app\lib\exception\ExceptionHandle' * */ class ExceptionHandler extends Handle{ private $code;//Http Code private $msg; private $errorCode; public function render(Exception $e){ if ($e instanceof BaseException) {//!!!!如果BaseException 与这个Exceptionhandler不是同一个命名空间一定要引入空间啊 $this->code=$e->code; $this->msg=$e->message; $this->errorCode=$e->errorCode; }else{ $this->code=500; $this->msg="服务器内部错误"; $this->errorCode=999;//自定义的哦         //这里手动记录日志 } $request=Request::instance(); $result=array( 'msg'=>$this->msg, 'error_code'=>$this->errorCode, 'request_url'=>$request->url() ); return json($result); } } ~~~ **为应用定义一个公共异常类** 然后新建application/lib/exception/BaseException.php baseException定义code msg errorCode这三个属性的默认值 ~~~ namespace app\lib\exception; use think\Exception; class BaseException extends Exception{ //将错误代码设为http状态码,业务错误代码设为errorCode public $code=400; //错误的具体信息 public $message="参数错误"; //自定义的业务错误码 public $errorCode="10000"; } //基类属性参考 protected string $message ;//异常消息内容 protected int $code ;//异常代码 protected string $file ;//抛出异常的文件名 protected int $line ;//抛出异常在该文件中的行号 ~~~ **每个模块每个错误类型定义一个异常类继承BaseException** application/lib/exception/BannerMissException.php ~~~ namespace app\lib\exception; //use app\lib\exception\Handle; class BannerMissException extends BaseException{ //http状态码 public $code=404; //错误的具体信息 public $message="请求的Banner不存在"; //自定义的错误码 public $errorCode=40000; } ~~~ ## **用法:** 在model中查询数据并在banner控制器调用 当model中没有数据时调用BannermissException 这时exceptionHandler的render将捕获这个错误 **模块** ~~~ namespace app\api\Model; use think\Model; // use think\Excption; class Banner extends Model{ public static function getBannerById($id){ //TODO:根据bannerid号获取banner信息 return null; //return "this is banner info"; } } ~~~ **控制器** ~~~ namespace app\api\controller\v1; use think\Controller; use app\api\Model\Banner as BannerModel;//这里的Banner和Model的Banner重名 use app\lib\exception\BannerMissException; class Banner extends controller{ public function index(){ //http://localhost/thinkphp5/public/index.php/api/v1.Banner/index } public function getBanner($id){ $banner=BannerModel::getBannerById($id); if (!$banner) { throw new BannerMissException(); } } } ~~~ ![](https://img.kancloud.cn/5b/d9/5bd955fdbed305bded89d94826c65ec1_800x313.png)