初始化提交
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
use Throwable;
|
||||
|
||||
class BusinessException extends ServerException
|
||||
{
|
||||
public function __construct(string $message = null, int $code = 500, Throwable $previous = null)
|
||||
{
|
||||
if (is_null($message)) {
|
||||
$message = HttpEnumCode::getMessage($code);
|
||||
}
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?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
|
||||
*/
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\Snowflake\IdGeneratorInterface;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Throwable;
|
||||
|
||||
class AppExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
#[Inject]
|
||||
protected StdoutLoggerInterface $logger;
|
||||
|
||||
protected string $environment;
|
||||
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(StdoutLoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->environment = env("APP_ENV", 'prod');
|
||||
}
|
||||
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
try {
|
||||
if ($this->environment != "dev"){
|
||||
// 生产环境 固定返回服务器错误
|
||||
$message = HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR);
|
||||
}else{
|
||||
$message = $throwable->getMessage();
|
||||
}
|
||||
|
||||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||||
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
|
||||
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
||||
$this->logger->error($throwable->getTraceAsString());
|
||||
return $response->withHeader('Server', 'gdxz')->withStatus(500)->withBody(new SwooleStream('服务器错误'));
|
||||
}
|
||||
|
||||
$id = $generator->generate();
|
||||
|
||||
$data = json_encode([
|
||||
'data' => $id,
|
||||
'code' => HttpEnumCode::SERVER_ERROR,
|
||||
'message' => $message,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$this->logger->error($id . "-start");
|
||||
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
||||
$this->logger->error($throwable->getTraceAsString());
|
||||
$this->logger->error($id . "-end");
|
||||
|
||||
return $response->withStatus(500)->withHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\Snowflake\IdGeneratorInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Throwable;
|
||||
|
||||
class BusinessExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
#[Inject]
|
||||
protected StdoutLoggerInterface $logger;
|
||||
|
||||
protected string $environment;
|
||||
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(StdoutLoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->environment = env("APP_ENV", 'prod');
|
||||
}
|
||||
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
// 判断被捕获到的异常是希望被捕获的异常
|
||||
if ($throwable instanceof BusinessException) {
|
||||
if ($this->environment != "dev"){
|
||||
// 生产环境 固定返回服务器错误
|
||||
$message = HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR);
|
||||
}else{
|
||||
$message = $throwable->getMessage();
|
||||
}
|
||||
|
||||
$generator = $this->container->get(IdGeneratorInterface::class);
|
||||
$id = $generator->generate();
|
||||
|
||||
$data = json_encode([
|
||||
'data' => $id,
|
||||
'code' => $throwable->getCode(),
|
||||
'message' => $message,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
$this->logger->error($id . "-start");
|
||||
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
|
||||
$this->logger->error($throwable->getTraceAsString());
|
||||
$this->logger->error($id . "-end");
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
return $response->withStatus(500)->withHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
|
||||
}
|
||||
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
|
||||
// 或者不做处理直接屏蔽异常
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断该异常处理器是否要对该异常进行处理
|
||||
*/
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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
|
||||
*/
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
//use App\Exception\ValidationException;
|
||||
use App\Constants\HttpEnumCode;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use \Hyperf\Validation\ValidationException;
|
||||
use Throwable;
|
||||
|
||||
class ValidationExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* @var StdoutLoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
public function __construct(StdoutLoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
// 判断被捕获到的异常是希望被捕获的异常
|
||||
if ($throwable instanceof ValidationException) {
|
||||
|
||||
$body = $throwable->validator->errors()->first();
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
// 格式化输出
|
||||
$data = json_encode([
|
||||
'data' => [],
|
||||
'code' => HttpEnumCode::CLIENT_HTTP_ERROR,
|
||||
'message' => $body,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
return $response->withStatus($throwable->status)->withHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
|
||||
}
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?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
|
||||
*/
|
||||
namespace App\Exception;
|
||||
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
|
||||
class ValidationException extends ServerException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user