优化
This commit is contained in:
@@ -5,13 +5,14 @@ declare(strict_types=1);
|
||||
namespace Aether\Exception;
|
||||
|
||||
use Aether\AetherValidator;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\Validation\ValidationException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
use Hyperf\Context\Context;
|
||||
|
||||
use function Hyperf\Support\env;
|
||||
|
||||
class AppExceptionHandler extends ExceptionHandler
|
||||
@@ -39,8 +40,13 @@ class AppExceptionHandler extends ExceptionHandler
|
||||
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一错误响应格式
|
||||
* 统一错误响应格式.
|
||||
*/
|
||||
private function formatErrorResponse(Throwable $throwable, string $requestId): array
|
||||
{
|
||||
@@ -61,21 +67,24 @@ class AppExceptionHandler extends ExceptionHandler
|
||||
'data' => env('APP_ENV') === 'dev' ? [
|
||||
'file' => $throwable->getFile(),
|
||||
'line' => $throwable->getLine(),
|
||||
'trace' => explode("\n", $throwable->getTraceAsString())
|
||||
'trace' => explode("\n", $throwable->getTraceAsString()),
|
||||
] : null,
|
||||
'request_id' => $requestId,
|
||||
'timestamp' => time()
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化自定义验证异常
|
||||
* 格式化自定义验证异常.
|
||||
*/
|
||||
private function formatValidationError(ValidationFailedException $e, string $requestId): array
|
||||
{
|
||||
// 复用AetherValidator的错误格式化方法
|
||||
$validatorInstance = new class extends AetherValidator {
|
||||
protected function scenes(): array { return []; }
|
||||
protected function scenes(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return [
|
||||
@@ -84,20 +93,23 @@ class AppExceptionHandler extends ExceptionHandler
|
||||
'data' => [
|
||||
'errors' => $validatorInstance->formatValidationErrors($e->validator),
|
||||
'scene' => $e->getScene(), // 直接从异常获取场景
|
||||
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null
|
||||
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null,
|
||||
],
|
||||
'request_id' => $requestId,
|
||||
'timestamp' => time()
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化原生验证异常(保持格式一致)
|
||||
* 格式化原生验证异常(保持格式一致).
|
||||
*/
|
||||
private function formatNativeValidationError(ValidationException $e, string $requestId): array
|
||||
{
|
||||
$validatorInstance = new class extends AetherValidator {
|
||||
protected function scenes(): array { return []; }
|
||||
protected function scenes(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return [
|
||||
@@ -105,15 +117,10 @@ class AppExceptionHandler extends ExceptionHandler
|
||||
'message' => '参数验证失败',
|
||||
'data' => [
|
||||
'errors' => $validatorInstance->formatValidationErrors($e->validator),
|
||||
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null
|
||||
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null,
|
||||
],
|
||||
'request_id' => $requestId,
|
||||
'timestamp' => time()
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user