diff --git a/app/Exceptions/ApiException.php b/app/Exceptions/ApiException.php index ab40d8a3b..0189b8dfd 100644 --- a/app/Exceptions/ApiException.php +++ b/app/Exceptions/ApiException.php @@ -10,13 +10,18 @@ class ApiException extends RuntimeException */ protected $data; + /** + * @var bool + */ + protected $writeLog = true; + /** * ApiException constructor. - * @param string $msg + * @param string|array $msg * @param array $data * @param int $code */ - public function __construct($msg = '', $data = [], $code = 0) + public function __construct($msg = '', $data = [], $code = 0, $writeLog = true) { if (is_array($msg) && isset($msg['code'])) { $code = $msg['code']; @@ -24,6 +29,7 @@ class ApiException extends RuntimeException $msg = $msg['msg']; } $this->data = $data; + $this->writeLog = $writeLog && $code !== -1; parent::__construct($msg, $code); } @@ -34,4 +40,12 @@ class ApiException extends RuntimeException { return $this->data; } + + /** + * @return bool + */ + public function isWriteLog(): bool + { + return $this->writeLog; + } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index b423bc0a7..b4cf3f267 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -74,7 +74,7 @@ class Handler extends ExceptionHandler public function report(Throwable $e) { if ($e instanceof ApiException) { - if ($e->getCode() !== -1) { + if ($e->isWriteLog()) { Log::error($e->getMessage(), [ 'code' => $e->getCode(), 'data' => $e->getData(), diff --git a/app/Module/Apps.php b/app/Module/Apps.php index 299c10930..1fcc0d75e 100644 --- a/app/Module/Apps.php +++ b/app/Module/Apps.php @@ -54,7 +54,7 @@ class Apps 'search' => 'ZincSearch', default => $appId, }; - throw new ApiException("应用「{$name}」未安装"); + throw new ApiException("应用「{$name}」未安装", [], 0, false); } } }