This commit is contained in:
Aether
2025-09-25 10:46:45 +08:00
parent 3c89a01a81
commit db760bb276
4 changed files with 46 additions and 24 deletions

View File

@@ -1,26 +1,21 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Aether\ApiExceptionHandler; use Aether\ApiExceptionHandler;
use Aether\GlobalExceptionHandler;
use Aether\RpcExceptionHandler; use Aether\RpcExceptionHandler;
use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler;
return [ return [
'handler' => [ 'handler' => [
'http' => [ 'http' => [
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, // HttpExceptionHandler::class,
ApiExceptionHandler::class, // ApiExceptionHandler::class,
GlobalExceptionHandler::class,
], ],
'jsonrpc-http' => [ 'jsonrpc-http' => [
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, HttpExceptionHandler::class,
RpcExceptionHandler::class, RpcExceptionHandler::class,
], ],
], ],

View File

@@ -119,9 +119,8 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
{ {
if (! static::query()->where('id', $id)->exists()) { if (! static::query()->where('id', $id)->exists()) {
throw new ModelNotFoundException(sprintf( throw new ModelNotFoundException(sprintf(
'找不到ID为 %d 的 %s 记录', '找不到 ID 为 %d 的记录',
$id, $id,
static::class
)); ));
} }
return static::query()->where('id', $id)->delete() > 0; return static::query()->where('id', $id)->delete() > 0;
@@ -155,9 +154,8 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
$record = static::query()->find($id); $record = static::query()->find($id);
if (is_null($record)) { if (is_null($record)) {
throw new ModelNotFoundException(sprintf( throw new ModelNotFoundException(sprintf(
'找不到ID为 %d 的 %s 记录', '找不到 ID 为 %d 的记录',
$id, $id
static::class
)); ));
} }
return $record; // static::query()->findOrFail($id); return $record; // static::query()->findOrFail($id);

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Aether;
class Config
{
public const RESPONSE_FIELD_KEY_CODE = 'code';
public const RESPONSE_FIELD_KEY_DATA = 'data';
public const RESPONSE_FIELD_KEY_MESSAGE = 'message';
}

View File

@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Aether; namespace Aether;
use Aether\Exception\BusinessException; use Aether\Exception\BusinessException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\MessageInterface; use Psr\Http\Message\MessageInterface;
@@ -15,15 +17,29 @@ use function Hyperf\Support\env;
class GlobalExceptionHandler extends ExceptionHandler 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 public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface
{ {
// 处理业务异常 // 处理业务异常
if ($throwable instanceof BusinessException) { if ($throwable instanceof BusinessException) {
$data = [ $data = [
'code' => $throwable->getCode(), Config::RESPONSE_FIELD_KEY_CODE => $throwable->getCode(),
'message' => $throwable->getMessage(), Config::RESPONSE_FIELD_KEY_MESSAGE => $throwable->getMessage(),
'data' => null, Config::RESPONSE_FIELD_KEY_DATA => null,
'timestamp' => time(), ];
}
// 数据库无记录异常
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 { } else {
// 记录未知错误日志 // 记录未知错误日志
@@ -36,15 +52,14 @@ class GlobalExceptionHandler extends ExceptionHandler
)); ));
$data = [ $data = [
'code' => 500, Config::RESPONSE_FIELD_KEY_CODE => 500,
'message' => 'Server internal error', Config::RESPONSE_FIELD_KEY_MESSAGE => 'Server internal error',
'data' => env('APP_ENV') === 'dev' ? [ Config::RESPONSE_FIELD_KEY_DATA => env('APP_ENV') === 'dev' ? [
'message' => $throwable->getMessage(), 'message' => $throwable->getMessage(),
'file' => $throwable->getFile(), 'file' => $throwable->getFile(),
'line' => $throwable->getLine(), 'line' => $throwable->getLine(),
'trace' => $throwable->getTraceAsString(), 'trace' => $throwable->getTraceAsString(),
] : null, ] : null,
'timestamp' => time(),
]; ];
} }