..
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
<?php
|
||||
|
||||
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\GlobalExceptionHandler;
|
||||
use Aether\RpcExceptionHandler;
|
||||
use Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler;
|
||||
|
||||
return [
|
||||
'handler' => [
|
||||
'http' => [
|
||||
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
|
||||
ApiExceptionHandler::class,
|
||||
// HttpExceptionHandler::class,
|
||||
// ApiExceptionHandler::class,
|
||||
GlobalExceptionHandler::class,
|
||||
],
|
||||
'jsonrpc-http' => [
|
||||
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
|
||||
HttpExceptionHandler::class,
|
||||
RpcExceptionHandler::class,
|
||||
],
|
||||
],
|
||||
|
||||
@@ -119,9 +119,8 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
|
||||
{
|
||||
if (! static::query()->where('id', $id)->exists()) {
|
||||
throw new ModelNotFoundException(sprintf(
|
||||
'找不到ID为 %d 的 %s 记录',
|
||||
'找不到 ID 为 %d 的记录',
|
||||
$id,
|
||||
static::class
|
||||
));
|
||||
}
|
||||
return static::query()->where('id', $id)->delete() > 0;
|
||||
@@ -155,9 +154,8 @@ abstract class AetherModel extends HyperfModel implements CacheableInterface
|
||||
$record = static::query()->find($id);
|
||||
if (is_null($record)) {
|
||||
throw new ModelNotFoundException(sprintf(
|
||||
'找不到ID为 %d 的 %s 记录',
|
||||
$id,
|
||||
static::class
|
||||
'找不到 ID 为 %d 的记录',
|
||||
$id
|
||||
));
|
||||
}
|
||||
return $record; // static::query()->findOrFail($id);
|
||||
|
||||
14
extend/Aether/PHP/Hyperf/Config.php
Normal file
14
extend/Aether/PHP/Hyperf/Config.php
Normal 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';
|
||||
}
|
||||
@@ -5,6 +5,8 @@ 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;
|
||||
@@ -15,15 +17,29 @@ 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 = [
|
||||
'code' => $throwable->getCode(),
|
||||
'message' => $throwable->getMessage(),
|
||||
'data' => null,
|
||||
'timestamp' => time(),
|
||||
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 {
|
||||
// 记录未知错误日志
|
||||
@@ -36,15 +52,14 @@ class GlobalExceptionHandler extends ExceptionHandler
|
||||
));
|
||||
|
||||
$data = [
|
||||
'code' => 500,
|
||||
'message' => 'Server internal error',
|
||||
'data' => env('APP_ENV') === 'dev' ? [
|
||||
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,
|
||||
'timestamp' => time(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user