feat : basic
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
|
||||
class AdminException extends ServerException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace App\Exception;
|
||||
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
|
||||
class ApiException extends ServerException
|
||||
class ErrException extends ServerException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Exception\AdminException;
|
||||
use App\Lib\AdminReturn;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class AdminExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* 注入
|
||||
* @param AdminReturn $return
|
||||
*/
|
||||
public function __construct(private readonly AdminReturn $return) {}
|
||||
|
||||
/**
|
||||
* admin控制器异常处理
|
||||
* @param Throwable $throwable
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($throwable instanceof AdminException) {
|
||||
$result = $this->return->error($throwable->getMessage(),$throwable->getCode());
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
return $response->withHeader("Content-Type", "application/json")
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Exception\ApiException;
|
||||
use App\Lib\ApiReturn;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Swow\Psr7\Message\ResponsePlusInterface;
|
||||
use Throwable;
|
||||
|
||||
class ApiExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* 注入
|
||||
* @param ApiReturn $return
|
||||
*/
|
||||
public function __construct(private readonly ApiReturn $return) {}
|
||||
|
||||
/**
|
||||
* @param Throwable $throwable
|
||||
* @param ResponsePlusInterface $response
|
||||
* @return ResponsePlusInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponsePlusInterface $response): ResponsePlusInterface
|
||||
{
|
||||
if ($throwable instanceof ApiException) {
|
||||
$result = $this->return->error($throwable->getMessage(),$throwable->getCode());
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
return $response->withHeader("Content-Type", "application/json")
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
64
app/Exception/Handler/ErrExceptionHandler.php
Normal file
64
app/Exception/Handler/ErrExceptionHandler.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use App\Exception\ErrException;
|
||||
use App\Lib\AdminReturn;
|
||||
use App\Lib\ApiReturn;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Hyperf\HttpServer\Request;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class ErrExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* 注入
|
||||
* @param Request $request
|
||||
* @param AdminReturn $adminReturn
|
||||
* @param ApiReturn $apiReturn
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly Request $request,
|
||||
private readonly AdminReturn $adminReturn,
|
||||
private readonly ApiReturn $apiReturn,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* admin控制器异常处理
|
||||
* @param Throwable $throwable
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($throwable instanceof ErrException) {
|
||||
$url = $this->request->path();
|
||||
$urlArr = explode('/',$url);
|
||||
|
||||
$result = match ($urlArr[0]) {
|
||||
'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()),
|
||||
'admin' => $this->adminReturn->error($throwable->getMessage(),$throwable->getCode()),
|
||||
default => null,
|
||||
};
|
||||
|
||||
// 阻止异常冒泡
|
||||
$this->stopPropagation();
|
||||
|
||||
if (!empty($result)) {
|
||||
return $response->withHeader("Content-Type", "application/json")
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
}
|
||||
|
||||
// 交给下一个异常处理器
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user