基础数据-校区服务
This commit is contained in:
45
app/Exception/BusinessException.php
Normal file
45
app/Exception/BusinessException.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use Hyperf\Server\Exception\ServerException;
|
||||
use Throwable;
|
||||
|
||||
class BusinessException extends ServerException
|
||||
{
|
||||
/**
|
||||
* 业务异常.
|
||||
* @param int $code 错误码
|
||||
* @param string $message 错误消息
|
||||
*/
|
||||
public function __construct(int $code = 500, string $message = '', ?Throwable $previous = null)
|
||||
{
|
||||
if (empty($message)) {
|
||||
$message = $this->getDefaultMessage($code);
|
||||
}
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认错误消息.
|
||||
*/
|
||||
private function getDefaultMessage(int $code): string
|
||||
{
|
||||
$messages = [
|
||||
400 => '请求参数错误',
|
||||
401 => '未授权',
|
||||
403 => '禁止访问',
|
||||
404 => '资源不存在',
|
||||
500 => '服务器内部错误',
|
||||
10001 => '校区不存在',
|
||||
10002 => '校区已存在',
|
||||
10003 => '父级校区不存在',
|
||||
10004 => '层级参数错误',
|
||||
];
|
||||
|
||||
return $messages[$code] ?? '业务处理失败';
|
||||
}
|
||||
}
|
||||
65
app/Exception/Handler/BusinessExceptionHandler.php
Normal file
65
app/Exception/Handler/BusinessExceptionHandler.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use Aether\AetherResponse;
|
||||
use App\Exception\BusinessException;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class BusinessExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* 显式声明next属性,确保存在.
|
||||
*/
|
||||
protected ?self $next = null;
|
||||
|
||||
/**
|
||||
* 处理异常.
|
||||
*/
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
// 只处理BusinessException类型的异常
|
||||
if ($throwable instanceof BusinessException) {
|
||||
$result = AetherResponse::error(
|
||||
$throwable->getCode(),
|
||||
$throwable->getMessage() ?: '业务处理异常'
|
||||
);
|
||||
|
||||
$body = json_encode($result, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus(200)
|
||||
->withBody(new SwooleStream((string) $body));
|
||||
}
|
||||
|
||||
// 安全处理下一个处理器
|
||||
if ($this->next !== null) {
|
||||
return $this->next->handle($throwable, $response);
|
||||
}
|
||||
|
||||
// 如果没有下一个处理器,直接返回原始响应
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否需要处理该异常.
|
||||
*/
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return $throwable instanceof BusinessException;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显式实现setNext方法.
|
||||
*/
|
||||
public function setNext(self $handler): void
|
||||
{
|
||||
$this->next = $handler;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user