异常啊异常

This commit is contained in:
Aether
2025-09-26 17:27:15 +08:00
parent d7880f99c9
commit afd384a52a
2 changed files with 6 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Aether; namespace Aether;
use Aether\Exception\BusinessException; use Aether\Exception\BusinessException;
use Aether\Exception\ValidationFailedException;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\Model\ModelNotFoundException; use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
@@ -26,6 +27,9 @@ class GlobalExceptionHandler extends ExceptionHandler
public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface
{ {
if ($throwable instanceof ValidationFailedException) {
return $response->withBody(new SwooleStream($throwable->getMessage()));
}
// 处理业务异常 // 处理业务异常
if ($throwable instanceof BusinessException) { if ($throwable instanceof BusinessException) {
$data = [ $data = [

View File

@@ -7,8 +7,6 @@ namespace Aether;
use Hyperf\Context\Context; use Hyperf\Context\Context;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Throwable; use Throwable;
@@ -16,10 +14,9 @@ use function Hyperf\Support\env;
class RpcExceptionHandler extends ExceptionHandler class RpcExceptionHandler extends ExceptionHandler
{ {
/**
*/
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{var_dump($throwable->getMessage()); {
var_dump($throwable->getMessage());
try { try {
// 获取请求ID用于日志追踪 // 获取请求ID用于日志追踪
$requestId = Context::get('request_id', ''); $requestId = Context::get('request_id', '');