fix(gateway): 优化接口异常处理

- 在 query 和 submit 方法中添加 try-catch 块,捕获异常并记录日志
- 新增 writelog 方法用于统一日志记录
- 异常发生时返回错误码 -100 和异常信息
This commit is contained in:
mzeros 2024-12-07 12:24:34 +08:00
parent 44fd4d4af5
commit d02041d686

View File

@ -3,6 +3,7 @@
namespace app\gateway\controller\v4; namespace app\gateway\controller\v4;
use app\channel\service\CardService; use app\channel\service\CardService;
use dever\Log;
/** /**
* 接口处理 * 接口处理
@ -17,6 +18,7 @@ class Handle extends Core
# 查询接口 一般用于查询数据,同步执行 # 查询接口 一般用于查询数据,同步执行
public function query(): void public function query(): void
{ {
try{
$settingStatus = sysconf('settingStatus'); $settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) { if ($settingStatus == 1) {
$this->no(-1000); $this->no(-1000);
@ -28,11 +30,17 @@ class Handle extends Core
} }
$data = $this->channel($this->mid, $this->product, false); $data = $this->channel($this->mid, $this->product, false);
$this->yes($data); $this->yes($data);
} catch (\Exception $e){
$this->writelog($e->getMessage(), 'error');
$this->no(-100,$e->getMessage());
}
} }
# 提交接口 一般用于提交数据,异步执行 # 提交接口 一般用于提交数据,异步执行
public function submit(): void public function submit(): void
{ {
try{
$settingStatus = sysconf('settingStatus'); $settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) { if ($settingStatus == 1) {
$this->no(-1000); $this->no(-1000);
@ -45,6 +53,12 @@ class Handle extends Core
$data = $this->channel($this->mid, $this->product, true); $data = $this->channel($this->mid, $this->product, true);
$this->yes($data); $this->yes($data);
} catch (\Exception $e){
$this->writelog($e->getMessage(), 'error');
$this->no(-100,$e->getMessage());
}
} }
# 提交接口 一般用于提交数据,异步执行 # 提交接口 一般用于提交数据,异步执行
@ -305,4 +319,14 @@ class Handle extends Core
return $v; return $v;
} }
protected function writelog($data,$t = '')
{
if($t){
Log::write('gateway', 'Handle_'.$t, $data);
}else{
Log::write('gateway', 'Handle', $data);
}
}
} }