Skip to content

Commit 4762efb

Browse files
committed
feat(super-magic-module): enhance error handling and add new error codes for workspace and task management
1 parent e49dd12 commit 4762efb

File tree

26 files changed

+682
-467
lines changed

26 files changed

+682
-467
lines changed

backend/magic-service/app/ErrorCode/SuperAgentErrorCode.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ enum SuperAgentErrorCode: int
1717
#[ErrorMessage('workspace.parameter_check_failure')]
1818
case VALIDATE_FAILED = 51000;
1919

20+
#[ErrorMessage('workspace.access_denied')]
21+
case WORKSPACE_ACCESS_DENIED = 51001;
22+
2023
#[ErrorMessage('topic.topic_not_found')]
2124
case TOPIC_NOT_FOUND = 51100;
25+
26+
#[ErrorMessage('topic.create_topic_failed')]
27+
case CREATE_TOPIC_FAILED = 51101;
28+
29+
#[ErrorMessage('task.not_found')]
30+
case TASK_NOT_FOUND = 51200;
31+
32+
#[ErrorMessage('task.work_dir_not_found')]
33+
case WORK_DIR_NOT_FOUND = 51201;
2234
}

backend/magic-service/config/autoload/error_message.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\ErrorCode\AsrErrorCode;
99
use App\ErrorCode\AuthenticationErrorCode;
1010
use App\ErrorCode\ChatErrorCode;
11+
use App\ErrorCode\EventErrorCode;
1112
use App\ErrorCode\FlowErrorCode;
1213
use App\ErrorCode\GenericErrorCode;
1314
use App\ErrorCode\HttpErrorCode;
@@ -33,6 +34,7 @@
3334
MagicApiErrorCode::class => [4000, 4100],
3435
MagicAccountErrorCode::class => [4100, 4300],
3536
GenericErrorCode::class => [5000, 9000],
37+
EventErrorCode::class => [6000, 6999],
3638
TokenErrorCode::class => [9000, 10000],
3739
FlowErrorCode::class => [31000, 31999],
3840
AgentErrorCode::class => [32000, 32999],

backend/super-magic-module/src/Application/Share/Service/ResourceShareAppService.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
use App\ErrorCode\ShareErrorCode;
1111
use App\Infrastructure\Core\Exception\BusinessException;
12+
use App\Infrastructure\Core\Exception\EventException;
1213
use App\Infrastructure\Core\Exception\ExceptionBuilder;
1314
use App\Interfaces\Authorization\Web\MagicUserAuthorization;
15+
use Dtyq\AsyncEvent\AsyncEventUtil;
1416
use Dtyq\SuperMagic\Application\Share\Factory\ShareableResourceFactory;
1517
use Dtyq\SuperMagic\Domain\Share\Constant\ResourceType;
1618
use Dtyq\SuperMagic\Domain\Share\Constant\ShareAccessType;
1719
use Dtyq\SuperMagic\Domain\Share\Entity\ResourceShareEntity;
20+
use Dtyq\SuperMagic\Domain\Share\Event\CreateShareBeforeEvent;
1821
use Dtyq\SuperMagic\Domain\Share\Service\ResourceShareDomainService;
1922
use Dtyq\SuperMagic\Infrastructure\Utils\AccessTokenUtil;
2023
use Dtyq\SuperMagic\Infrastructure\Utils\PasswordCrypt;
@@ -65,6 +68,11 @@ public function createShare(MagicUserAuthorization $userAuthorization, CreateSha
6568
// 1. 获取对应类型的资源工厂
6669
try {
6770
$factory = $this->resourceFactory->create($resourceType);
71+
72+
// 发送创建分享事件
73+
AsyncEventUtil::dispatch(new CreateShareBeforeEvent($userAuthorization->getOrganizationCode(), $userAuthorization->getId(), $dto->getResourceId(), $dto->getResourceType()->value));
74+
} catch (EventException $e) {
75+
ExceptionBuilder::throw(ShareErrorCode::PERMISSION_DENIED, $e->getMessage());
6876
} catch (RuntimeException $e) {
6977
// 使用 ExceptionBuilder 抛出不支持的资源类型异常
7078
ExceptionBuilder::throw(ShareErrorCode::RESOURCE_TYPE_NOT_SUPPORTED, 'share.resource_type_not_supported', [$resourceType->name]);

backend/super-magic-module/src/Application/SuperAgent/Crontab/CheckTaskStatusTask.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Dtyq\SuperMagic\Application\SuperAgent\Crontab;
99

10+
use Dtyq\AsyncEvent\AsyncEventUtil;
1011
use Dtyq\SuperMagic\Domain\SuperAgent\Entity\ValueObject\TaskStatus;
12+
use Dtyq\SuperMagic\Domain\SuperAgent\Event\RunTaskAfterEvent;
1113
use Dtyq\SuperMagic\Domain\SuperAgent\Service\TaskDomainService;
1214
use Dtyq\SuperMagic\Domain\SuperAgent\Service\TopicDomainService;
1315
use Dtyq\SuperMagic\Infrastructure\ExternalAPI\Sandbox\SandboxInterface;
@@ -104,6 +106,9 @@ private function checkTasksStatus(): void
104106

105107
// 更新话题状态
106108
$this->topicDomainService->updateTopicStatus($topic->getId(), $taskId, TaskStatus::ERROR);
109+
110+
// 触发完成事件
111+
AsyncEventUtil::dispatch(new RunTaskAfterEvent($topic->getUserOrganizationCode(), $topic->getUserId(), $topic->getId(), $taskId, TaskStatus::ERROR->value, null));
107112
++$updatedToErrorCount;
108113
}
109114

backend/super-magic-module/src/Application/SuperAgent/Event/Subscribe/TopicTaskMessageSubscriber.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function consumeMessage($data, AMQPMessage $message): Result
7777
$applicationHeaders = $messageProperties['application_headers'] ?? new AMQPTable([]);
7878
// 直接从原生数据中获取,如果不存在则为 null
7979
$originalTimestampFromHeader = $applicationHeaders->getNativeData()['x-original-timestamp'] ?? null;
80+
8081
$currentTimeForLog = time(); // 当前处理时间,主要用于日志和可能的本地逻辑
8182
$actualOriginalTimestamp = null; // 初始化变量以避免 linter 警告
8283

@@ -98,9 +99,6 @@ public function consumeMessage($data, AMQPMessage $message): Result
9899
// 验证消息格式
99100
$this->validateMessageFormat($data);
100101

101-
// 打印消息详情,用于测试和验证 (根据需要取消注释)
102-
// $this->logMessageDetails($data);
103-
104102
// 创建DTO
105103
$messageDTO = TopicTaskMessageDTO::fromArray($data);
106104

@@ -140,6 +138,7 @@ public function consumeMessage($data, AMQPMessage $message): Result
140138
date('Y-m-d H:i:s', $actualOriginalTimestamp),
141139
$messageDTO->getPayload()?->getMessageId()
142140
));
141+
143142
try {
144143
$this->superAgentAppService->handleTopicTaskMessage($messageDTO);
145144
return Result::ACK;

backend/super-magic-module/src/Application/SuperAgent/Service/FileProcessAppService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use App\Application\Chat\Service\MagicChatFileAppService;
1111
use App\Application\File\Service\FileAppService;
1212
use App\Domain\Contact\Entity\ValueObject\DataIsolation;
13-
use App\Domain\File\Service\FileDomainService;
1413
use App\ErrorCode\GenericErrorCode;
14+
use App\ErrorCode\SuperAgentErrorCode;
1515
use App\Infrastructure\Core\Exception\ExceptionBuilder;
1616
use App\Infrastructure\Core\ValueObject\StorageBucketType;
1717
use App\Interfaces\Authorization\Web\MagicUserAuthorization;
@@ -22,7 +22,6 @@
2222
use Hyperf\Codec\Json;
2323
use Hyperf\Logger\LoggerFactory;
2424
use Psr\Log\LoggerInterface;
25-
use Psr\SimpleCache\CacheInterface;
2625
use Throwable;
2726

2827
/**
@@ -349,11 +348,11 @@ public function refreshStsToken(RefreshStsTokenRequestDTO $requestDTO): array
349348
// 获取 task 表中的 work_dir 目录作为工作目录
350349
$taskEntity = $this->taskDomainService->getTaskById((int) $requestDTO->getSuperMagicTaskId());
351350
if (empty($taskEntity)) {
352-
ExceptionBuilder::throw(GenericErrorCode::SystemError, 'task.not_found');
351+
ExceptionBuilder::throw(SuperAgentErrorCode::TASK_NOT_FOUND, 'task.not_found');
353352
}
354353
$workDir = $taskEntity->getWorkDir();
355354
if (empty($workDir)) {
356-
ExceptionBuilder::throw(GenericErrorCode::SystemError, 'task.work_dir.not_found');
355+
ExceptionBuilder::throw(SuperAgentErrorCode::WORK_DIR_NOT_FOUND, 'task.work_dir.not_found');
357356
}
358357

359358
// 获取STS临时凭证

0 commit comments

Comments
 (0)