任务时间: 2025年06月09日 16:52
任务类型: Bug修复
影响模块: 游戏物品合成系统
用户反馈合成功能出错,在验证通过后服务层处理时还会出错,验证逻辑不完善。从日志分析发现:
CraftHandler中没有使用ItemCraftValidation进行完整验证,只是做了基础的参数验证,导致:
发现多个错误处理的问题:
ItemRecipe::canCraftByUser方法中使用了$consumeResult->error而不是!$consumeResult->successCraftService::craftItem方法中使用了$consumeResult->error而不是!$consumeResult->successCraftHandler中使用了$result->error而不是!$result->successException而不是\Exception从错误日志可以看到:
[2025-06-09T16:43:15.356041+08:00] laravel.ERROR: 合成物品失败 {"user_id":10001,"recipe_id":6,"error":"","trace":"..."}
错误信息为空字符串,说明异常没有被正确捕获和处理。
文件: app/Module/AppGame/Handler/Item/CraftHandler.php
// 添加验证类引用
use App\Module\GameItems\Validations\ItemCraftValidation;
// 在处理逻辑中添加验证
// 进行完整的合成验证
$validation = new ItemCraftValidation([
'user_id' => $userId,
'recipe_id' => $recipeId,
'quantity' => $quantity
]);
// 验证数据,如果验证失败会抛出异常
$validation->validated();
文件: app/Module/GameItems/Models/ItemRecipe.php
// 修复第217行
if (!$consumeResult->success) { // 原来是 $consumeResult->error
return [
'can_craft' => false,
'reason' => '材料不足: ' . $consumeResult->message,
];
}
文件: app/Module/GameItems/Services/CraftService.php
// 修复第78行
if (!$consumeResult->success) { // 原来是 $consumeResult->error
throw new LogicException("消耗失败: " . $consumeResult->message);
}
// 修复异常捕获
} catch (\Exception $e) { // 原来是 Exception $e
文件: app/Module/AppGame/Handler/Item/CraftHandler.php
// 修复第61行
if (!$result->success) { // 原来是 $result->error
throw new LogicException($result->message); // 使用具体的错误信息
}
{
"响应内容": null,
"日志错误": "合成物品失败 {\"error\":\"\"}",
"用户体验": "无法获得具体错误信息"
}
{
"runUnid": "6846a0e96375f",
"runMs": "275",
"code": "VALIDATE_ERROR",
"msg": "物品 5 数量不足,需要 100,实际 0",
"callpath": "Item-Craft",
"lastData": []
}
VALIDATE_ERROR,表明是验证阶段的错误app/Module/AppGame/Handler/Item/CraftHandler.php
app/Module/GameItems/Models/ItemRecipe.php
app/Module/GameItems/Services/CraftService.php
使用复现命令验证修复效果:
php artisan debug:reproduce-error 68469e705a737
修复前:返回null,错误信息为空 修复后:返回具体错误信息,用户体验良好
通过在Handler层添加完整的验证逻辑,并修复多个错误处理问题,成功解决了验证通过后服务层还会出错的问题。现在系统能够在验证阶段就捕获到材料不足等问题,避免了在事务中出错,提升了用户体验和系统稳定性。