优化
This commit is contained in:
@@ -5,14 +5,15 @@ declare(strict_types=1);
|
||||
namespace Aether\Exception;
|
||||
|
||||
use Aether\AetherValidator;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Database\Model\ModelNotFoundException; // 引入模型未找到异常
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\StdoutLoggerInterface; // 引入模型未找到异常
|
||||
use Hyperf\Database\Model\ModelNotFoundException;
|
||||
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 AetherExceptionHandler extends ExceptionHandler
|
||||
@@ -52,6 +53,11 @@ class AetherExceptionHandler 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,10 +67,10 @@ class AetherExceptionHandler extends ExceptionHandler
|
||||
'message' => $throwable->getMessage() ?: '请求的资源不存在',
|
||||
'data' => env('APP_ENV') === 'dev' ? [
|
||||
'file' => $throwable->getFile(),
|
||||
'line' => $throwable->getLine()
|
||||
'line' => $throwable->getLine(),
|
||||
] : null,
|
||||
'request_id' => $requestId,
|
||||
'timestamp' => time()
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -85,17 +91,20 @@ class AetherExceptionHandler 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
|
||||
{
|
||||
$validatorInstance = new class extends AetherValidator {
|
||||
protected function scenes(): array { return []; }
|
||||
protected function scenes(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return [
|
||||
@@ -104,17 +113,20 @@ class AetherExceptionHandler 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 [
|
||||
@@ -122,15 +134,10 @@ class AetherExceptionHandler 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