76 lines
2.5 KiB
PHP
76 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Aether;
|
|
|
|
use Aether\Exception\BusinessException;
|
|
use Hyperf\Contract\StdoutLoggerInterface;
|
|
use Hyperf\Database\Model\ModelNotFoundException;
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
use Psr\Http\Message\MessageInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
|
|
use function Hyperf\Support\env;
|
|
|
|
class GlobalExceptionHandler extends ExceptionHandler
|
|
{
|
|
protected StdoutLoggerInterface $logger;
|
|
|
|
public function __construct(StdoutLoggerInterface $logger)
|
|
{
|
|
$this->logger = $logger; // filter
|
|
}
|
|
|
|
public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface
|
|
{
|
|
// 处理业务异常
|
|
if ($throwable instanceof BusinessException) {
|
|
$data = [
|
|
Config::RESPONSE_FIELD_KEY_CODE => $throwable->getCode(),
|
|
Config::RESPONSE_FIELD_KEY_MESSAGE => $throwable->getMessage(),
|
|
Config::RESPONSE_FIELD_KEY_DATA => null,
|
|
];
|
|
}
|
|
// 数据库无记录异常
|
|
if ($throwable instanceof ModelNotFoundException) {
|
|
$data = [
|
|
Config::RESPONSE_FIELD_KEY_CODE => 404,
|
|
Config::RESPONSE_FIELD_KEY_MESSAGE => $throwable->getMessage() ?: '没有对应记录',
|
|
Config::RESPONSE_FIELD_KEY_DATA => null,
|
|
];
|
|
} else {
|
|
// 记录未知错误日志
|
|
$this->logger->error(sprintf(
|
|
'Unknown error: %s %s in %s:%d',
|
|
$throwable->getMessage(),
|
|
$throwable->getCode(),
|
|
$throwable->getFile(),
|
|
$throwable->getLine()
|
|
));
|
|
|
|
$data = [
|
|
Config::RESPONSE_FIELD_KEY_CODE => 500,
|
|
Config::RESPONSE_FIELD_KEY_MESSAGE => 'Server internal error',
|
|
Config::RESPONSE_FIELD_KEY_DATA => env('APP_ENV') === 'dev' ? [
|
|
'message' => $throwable->getMessage(),
|
|
'file' => $throwable->getFile(),
|
|
'line' => $throwable->getLine(),
|
|
'trace' => $throwable->getTraceAsString(),
|
|
] : null,
|
|
];
|
|
}
|
|
|
|
$body = json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
return $response->withHeader('Content-Type', 'application/json')
|
|
->withBody(new SwooleStream($body));
|
|
}
|
|
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|