您现在的位置是:网站首页>软件开发>开发终端>thinkphpthinkphp

设计一个完整合理的自定义异常处理类库,要求在非调试模式下不能抛出含具体错误代码的页面,要求能分型处理服务器端异常和客户端异常。

风口下的猪2019-07-16thinkphp

简介

(1)修改日志记录位置,并且关闭默认日志记录

        入口文件index.php中定义LOG_PATH常量

define('LOG_PATH',_DIR_.'/../log/');

        配置文件config.php中设置开关

'log'=>['type'=>'file'];

将其改

为'log'=>['type'=>'test'];


(2)将自定义异常处理当作类库,供其它模块调用,建lib文件夹/exception文件夹,下设三个基础类:ServerException(服务器端异常处理源类)、ClientException(客户端异常源类)、ExceptionHandler(异常处理方法类)。


一.ServerException.php

namespace app\lib\exception;
use think\Exception;

class ServerException extends Exception{
       public $code=500;
       public $msg='服务器内部错误,不想告述你';
       public $errorCode=999;
       public function __construction ($params=[]){
            if(!is_array($params)){
                return ;
            }
            if(array_key_exists('code',$params)){
                $this->code=$params['code'];
            }
            if(array_key_exists('msg',$params)){
                $this->msg=$params['msg'];
            }
            if(array_key_exists('errorCode',$params)){
                $this->errorCode=$params['errorCode'];
            }
       }
}



二.ClientException.php

namespace app\lib\exception;
use think\Exception;

class ClientException extends Exception{
       public $code=400;
       public $msg='参数错误';
       public $errorCode=1000;
       public function __construction ($params=[]){
            if(!is_array($params)){
                return ;
            }
            if(array_key_exists('code',$params)){
                $this->code=$params['code'];
            }
            if(array_key_exists('msg',$params)){
                $this->msg=$params['msg'];
            }
            if(array_key_exists('errorCode',$params)){
                $this->errorCode=$params['errorCode'];
            }
       }
}



三.ExceptionHandler.php

namespace app\lib\exception;
use think\Exception;
use think\exception\Handle;
use think\Request;

class ExceptionHandler extends Handle{
       private $code;
       private $msg;
       private $errorCode;
       public function render(Exception $e){
            if($e instanceof ClientException){ //之前是BaceException
                //如果是自定义的异常
                $this->code=$e->code;
                $this->msg=$e->msg;
                $this->errorCode=$e->errorCode;
            }else{
                //如果是服务器端内部代码错误  
                if(config('app_debug')){
               //使用系统内置Exception类中的render()异常处理方法,就可以显示具体的错误信息(原本调试模式就是采用这样的方式)
                     return  parent::render($e);
                }else{
                     $this->code=$e->code;
                     $this->msg=$e->msg;
                     $this->errorCode=$e->errorCode;
                     $this->recordErrorLog($e);
                }
            }
            $request=Request::instance();
            $result=[
              'msg'=>$this->msg,
              'error_code'=>$this->errorCode,
              'request_url'=>$request->url()
            ];
            return json($result,$this->code);
       }
       private function recordErrorLog(Exception $e){
              //log类的初始化
            Log::init([
                     'type'=>'file',
                     'path'=>LOG_PATH,
                     'level'=>['error']
            ]);
              //log类的写入函数
            Log::record($e->getMessege,'error');
       }

}


(3)设置系统异常处理走自定义类库

config.php中设置

 'exception_handle'=>'app\lib\exception\ExceptionHander.php',


(4)定义服务器端异常处理系列类和客户端异常处理系列类

这里以参数错误(客户端异常处理)和请求资源没找到(服务器端异常处理)为例

1.ParameterException.php

namespace app\lib\exception;
use think\Exception;

class ParameterException extends ClientException{
       public $code=400;
       public $msg='参数错误';
       public $errorCode=1000;

}

2.MissException.php

namespace app\lib\exception;
use think\Exception;

class MissException extends ServerException{
       public $code=500;
       public $msg='请求资源没有找到';
       public $errorCode=900;
}



(5)使用两种系列的异常处理

$e= new  ParameterException([
      'msg'=>$this->error;
]);
throw $e;


很赞哦! (0)

  • 软件开发
  • 素质要求
  • 计算机基础
  • 架构
  • 安全
  • 性能
  • 运维
  • 尾页
  • 数据库
  • 开发终端
  • 语言基础
  • 项目管理
  • 产品设计
  • 系统
  • 工作规范
  • 计算机网络
  • 前端技术栈
  • 数据结构
  • 计算机组成原理
  • 后端技术栈
  • 性能优化
  • 安全设计
  • 常见模块
  • 计算机操作系统
  • 服务器
  • python
  • MySQL
  • thinkphp
  • PHP
  • Java
  • JavaScript
  • Windows
  • Linux
  • 特效
  • indexedDB
  • vue
  • 淘宝联盟
  • Ionic
  • Angular
  • 微信小程序
  • 支付宝小程序
  • uni-app
  • css/sass/less
  • 支付
  • socket
  • 爬虫
  • web性能优化
  • 消息推送
  • CVM
  • sqlite
  • Redis
  • 前端基础
  • 基础
  • element
  • Nginx
  • yii2
  • /ponder/index.php/index/catelist/catelist/cateid/10.html

    相关阅读 (同一栏目)

    << /

    标签云

    站点信息

    • 文章统计:528篇
    • 移动端访问:扫码进入SQ3R