123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace app\common\service;
- use think\exception\Handle;
- use think\exception\HttpException;
- use think\exception\ValidateException;
- use think\Request;
- /**
- * TP5异常捕获托管类
- * Class MyException
- * @package app\common\service
- */
- class MyException extends Handle
- {
- public function render(\Exception $e)
- {
- // 参数验证错误
- if ($e instanceof ValidateException) {
- die(json_encode(['code'=>400,'msg'=>$e->getError(),'data'=>[]]));
- }
- // 请求异常
- if ($e instanceof HttpException && request()->isAjax()) {
- die(json_encode(['code'=>$e->getStatusCode(),'msg'=>$e->getMessage(),'data'=>[]]));
- }
- $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
- //TODO::开发者对异常的操作
- $input = file_get_contents("php://input");
- $errData = [
- 'requestUrl'=>$url,
- 'requestParam'=>['request'=>$_REQUEST,'input'=>@json_decode($input,true),'old_input'=>$input],
- 'msg'=>$e->getMessage(),
- 'line'=>$e->getLine(),
- 'path'=>$e->getFile(),
- ];
-
- if(strpos($url, "debug=xiepeng123@")){
- //可以在此交由系统处理
- return parent::render($e);
- }
-
- $this->pushRedis($errData);
- die(json_encode((['msg'=>$e->getMessage(),'code'=>400,'data'=>[]])));
- }
- public function pushRedis($data){
- try {
- $request = Request::instance();
- $data = [
- "companyCode"=>"CHENSEN",
- "projectCode"=>"CSAPI",
- "requestUrl"=>$data['requestUrl'],
- "requestParam"=>$data['requestParam'],
- "msg"=>$data['msg'],
- "line"=>$data['line'],
- "path"=>$data['path'],
- "requestTime"=>microtime(true),
- "_client_ip_"=>$request->ip(0,true)
- ];
- $redis = new \Redis();
- $redis->connect('47.97.187.118', 6379,2);
- $redis->auth('gudong-hz');
- $redis->rpush('email_list', json_encode($data));
- }catch (\Exception $ex){
- return true;
- }
- }
- }
|