异常啊异常

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

View File

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