优化业务错误顶层响应
This commit is contained in:
@@ -22,6 +22,43 @@ bool isRobotCommand(const std::string &req_cmd)
|
||||
req_cmd == "Cmd_SelectCraftTree" ||
|
||||
req_cmd == "Cmd_AddOperationTree";
|
||||
}
|
||||
|
||||
// 从业务返回体推导顶层 API 是否成功,避免把业务错误包装成成功响应。
|
||||
bool isBusinessResultSuccess(const json &res_data)
|
||||
{
|
||||
if (!res_data.is_object())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (res_data.contains("success") && res_data["success"].is_boolean())
|
||||
{
|
||||
return res_data["success"].get<bool>();
|
||||
}
|
||||
|
||||
return !res_data.contains("error");
|
||||
}
|
||||
|
||||
// 提取业务错误信息,用于同步顶层 msg 字段。
|
||||
std::string getBusinessResultMessage(const json &res_data, const std::string &fallback)
|
||||
{
|
||||
if (isBusinessResultSuccess(res_data))
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
|
||||
if (res_data.contains("message") && res_data["message"].is_string())
|
||||
{
|
||||
return res_data["message"].get<std::string>();
|
||||
}
|
||||
|
||||
if (res_data.contains("error") && res_data["error"].is_string())
|
||||
{
|
||||
return res_data["error"].get<std::string>();
|
||||
}
|
||||
|
||||
return fallback.empty() ? "Business command failed" : fallback;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
KinematicsWebAPI::KinematicsWebAPI()
|
||||
@@ -62,7 +99,10 @@ std::string KinematicsWebAPI::func(std::string sanitized_body)
|
||||
const json req_param = request_json.value("req_param", json::object());
|
||||
|
||||
json res_data = dispatchCommand(req_cmd, req_param);
|
||||
return utils::create_api_response(true, 0, msg, req_code, req_from, req_cmd, res_data).dump();
|
||||
const bool business_success = isBusinessResultSuccess(res_data);
|
||||
const int code = business_success ? 0 : 1000;
|
||||
const std::string response_msg = getBusinessResultMessage(res_data, msg);
|
||||
return utils::create_api_response(business_success, code, response_msg, req_code, req_from, req_cmd, res_data).dump();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
|
||||
@@ -703,9 +703,32 @@ json SpcCalculator::Spc(const json ¶m)
|
||||
// 使用测试数据
|
||||
const std::vector<double> &testData = req_param.x; // 改为小写 x
|
||||
int subgroupSize = req_param.n;
|
||||
int subgroupCount = req_param.k;
|
||||
double usl = req_param.usl;
|
||||
double lsl = req_param.lsl;
|
||||
|
||||
if (subgroupSize < MIN_SUBGROUP_SIZE || subgroupSize > MAX_SUBGROUP_SIZE)
|
||||
{
|
||||
throw std::invalid_argument("子组大小 n 必须在 2 到 25 之间");
|
||||
}
|
||||
|
||||
if (subgroupCount <= 0)
|
||||
{
|
||||
throw std::invalid_argument("子组个数 k 必须大于 0");
|
||||
}
|
||||
|
||||
if (testData.empty())
|
||||
{
|
||||
throw std::invalid_argument("测量数据 x 不能为空");
|
||||
}
|
||||
|
||||
const size_t expectedDataCount = static_cast<size_t>(subgroupSize) * static_cast<size_t>(subgroupCount);
|
||||
if (testData.size() != expectedDataCount)
|
||||
{
|
||||
throw std::invalid_argument("数据个数不匹配:期望 n*k=" + std::to_string(expectedDataCount) +
|
||||
",实际 x=" + std::to_string(testData.size()));
|
||||
}
|
||||
|
||||
// 计算并获取整合的 JSON
|
||||
SpcDataXR xr = CalculateXR(testData, subgroupSize);
|
||||
SpcDataXS xs = CalculateXS(testData, subgroupSize);
|
||||
@@ -1419,4 +1442,4 @@ int main_spc()
|
||||
std::cerr << "错误: " << ex.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user