Commit 943abd12 by zhufx

错误调用路径封装

parent 1b3c40b4
...@@ -11,3 +11,4 @@ temp/ ...@@ -11,3 +11,4 @@ temp/
.phpintel/ .phpintel/
.env .env
.DS_Store .DS_Store
/vendor
...@@ -15,7 +15,7 @@ class CommonException extends Exception ...@@ -15,7 +15,7 @@ class CommonException extends Exception
public $errorCode = -1; public $errorCode = -1;
//异常调用路径 //异常调用路径
protected $path; protected $path=[];
public function __construct($params = []) public function __construct($params = [])
{ {
...@@ -36,9 +36,16 @@ class CommonException extends Exception ...@@ -36,9 +36,16 @@ class CommonException extends Exception
} }
if ( isset($params['path']) ) { if ( isset($params['path']) ) {
$this->path = json_encode($params['path']); $this->path = $params['path'];
} }
//将此次异常信息入栈
array_unshift($this->path,'Service:'.APP_NAME.' file:'.$this->file.$this->line)//将本次错误调用加入path
//json序列化path
&& $this->path = json_encode($this->path);
parent::__construct($this->message, $this->code); parent::__construct($this->message, $this->code);
} }
......
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/8/3
* Time: 17:21
*/
namespace Hdll\Services\Common\Exception;
class ExceptionData
{
public static function getExceptionData($errData, array $param= [])
{
return [
"code" => $errData['code'],
"msg" => $errData['msg'],
"errorCode" => $errData["errCode"],
"data" => $errData['data'],
"path" => $errData['path']
];
}
/**
* 组装抛出的rpc信息
* @param \Throwable $e
* @return array
*/
public static function getRpcErrData(\Throwable $e)
{
//初始化异常数据
$data = [
"code" => $e->getCode(),
"msg" => $e->getMessage(),
"errCode" => is_callable([$e,'getErrCode'])?$e->getErrcode():'',
'path' =>[],
];
if ( is_callable([$e,"getPath"]) ) {
$data['path'] = json_decode($e->getPath(), true);
}
//获取调用堆栈信息
$info = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,1)[0];
//将此次异常信息入站
array_unshift($data['path'],'Service:'.APP_NAME.' file:'.$info['file'].$info['line']);
return ['msg' => json_encode($data), 'code' => $data['code']];
}
}
\ No newline at end of file
...@@ -11,9 +11,10 @@ class ExceptionParseData ...@@ -11,9 +11,10 @@ class ExceptionParseData
{ {
public static function parseData(\Exception $e) public static function parseData(\Exception $e)
{ {
if ( is_callable($e,'getResponse') ) { if ( is_callable([$e,'getResponse']) ) {
$response = $e->getResponse(); $response = $e->getResponse();
preg_match("/{.*}/",$response['msg'],$data); preg_match("/{.*}/",$response['msg'],$data);
$msg = isset($data['0'])?$data['0']:''; $msg = isset($data['0'])?$data['0']:'';
...@@ -22,6 +23,7 @@ class ExceptionParseData ...@@ -22,6 +23,7 @@ class ExceptionParseData
$data['msg'] = $e->getMessage(); $data['msg'] = $e->getMessage();
$data['code'] = $e->getCode(); $data['code'] = $e->getCode();
$data['file'] = $e->getFile().' '.$e->getLine(); $data['file'] = $e->getFile().' '.$e->getLine();
$data['path'] = [$e->getFile().' '.$e->getLine()];
} }
return $data; return $data;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment