This commit is contained in:
2026-07-30 10:21:00 +08:00
parent d6db7bf905
commit b6e3ce97b4
5 changed files with 40 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ use Psr\Http\Message\ResponseInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\RpcClient\Exception\RequestException;
use Dhd\Micro\Core\Exception\BusinessException;
use Dhd\Micro\Core\Trait\RpcContextTrait;
@@ -43,12 +44,16 @@ class GatewayExceptionHandler extends ExceptionHandler
}
$request = Util::get(RequestInterface::class);
Util::logger()->setErrorStatus(!($throwable instanceof BusinessException))
Util::logger()
->setErrorStatus(
!($throwable instanceof BusinessException)
&& !($throwable instanceof RequestException)
)
->setMessage($message)
->setThrowable($throwable)
->setTraceId($traceId)
->setContext([
'url' => $request->getUri(),
'url' => $request->url(),
'method' => $request->getMethod(),
'request_data' => $request->all(),
])

View File

@@ -15,7 +15,9 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\RpcClient\Exception\RequestException;
use Dhd\Micro\Core\Exception\BusinessException;
use Dhd\Micro\Core\Exception\ValidateException;
use Dhd\Micro\Core\Trait\RpcContextTrait;
@@ -36,7 +38,12 @@ class JsonRpcBusinessExceptionHandler extends ExceptionHandler
$code = $throwable->getCode() ?? 500;
$message = $throwable->getMessage() ?? '服务器内部错误';
Util::logger()->setErrorStatus(!($throwable instanceof BusinessException))
Util::logger()
->setErrorStatus(
!($throwable instanceof BusinessException)
&& !($throwable instanceof ValidateException)
&& !($throwable instanceof RequestException)
)
->setMessage($message)
->setThrowable($throwable)
->setTraceId($traceId)

View File

@@ -0,0 +1,21 @@
<?php
/**
* 验证异常
*/
declare(strict_types=1);
namespace Dhd\Micro\Core\Exception;
use RuntimeException;
class ValidateException extends RuntimeException
{
protected $message = '参数验证错误';
public function __construct(string $message = '')
{
parent::__construct($message, 422);
}
}

View File

@@ -4,6 +4,8 @@
* 日志记录
*/
declare(strict_types=1);
namespace Dhd\Micro\Core\Library;
use Hyperf\Logger\LoggerFactory;

View File

@@ -4,12 +4,13 @@
* 公共函数库
*/
declare(strict_types=1);
namespace Dhd\Micro\Core\Library;
use Hyperf\Context\ApplicationContext;
use Hyperf\Collection\Arr;
use Hyperf\Carbon\Carbon;
use Hyperf\Logger\LoggerFactory;
use Dhd\Micro\Core\Library\Logger;
final class Util