优化
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,25 +5,30 @@ declare(strict_types=1);
|
||||
namespace Aether\Exception;
|
||||
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
use Throwable;
|
||||
|
||||
class BusinessException extends ServerException
|
||||
{
|
||||
// 错误码常量(按业务模块划分)
|
||||
public const VALIDATION_ERROR = 400; // 参数验证失败
|
||||
|
||||
public const AUTH_ERROR = 401; // 认证失败
|
||||
|
||||
public const PERMISSION_DENY = 403; // 权限不足
|
||||
|
||||
public const RESOURCE_NOT_FOUND = 404; // 资源不存在
|
||||
|
||||
public const SCENE_NOT_FOUND = 400;
|
||||
|
||||
/**
|
||||
* 额外错误数据(如验证详情)
|
||||
* 额外错误数据(如验证详情).
|
||||
*/
|
||||
protected ?array $errorData = null;
|
||||
|
||||
public function __construct(
|
||||
string $message,
|
||||
int $code = 500,
|
||||
?\Throwable $previous = null,
|
||||
?Throwable $previous = null,
|
||||
?array $errorData = null
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
@@ -31,10 +36,10 @@ class BusinessException extends ServerException
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取额外错误数据
|
||||
* 获取额外错误数据.
|
||||
*/
|
||||
public function getErrorData(): ?array
|
||||
{
|
||||
return $this->errorData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Aether\Exception;
|
||||
|
||||
use Hyperf\Validation\ValidationException;
|
||||
@@ -9,7 +11,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class ValidationFailedException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* 验证场景
|
||||
* 验证场景.
|
||||
*/
|
||||
protected string $scene;
|
||||
|
||||
@@ -30,4 +32,4 @@ class ValidationFailedException extends ValidationException
|
||||
{
|
||||
return $this->scene;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user