diff --git a/include/restool_errors.h b/include/restool_errors.h index b218903fd80666dc7431bf38a46d59f2c6783a90..6ed2694eb15bbbea5a7c8ce675ea02fb27517d2d 100644 --- a/include/restool_errors.h +++ b/include/restool_errors.h @@ -134,6 +134,17 @@ struct MoreInfo { std::string en; }; +struct ExtSolution { + std::string fileName; + std::string solution; +}; + +struct FaqInfo { + std::string cn; + std::string en; + std::vector extSolutions; +}; + class ErrorInfo { public: uint32_t code_; diff --git a/restool_faq.json b/restool_faq.json index 2cbfe1f65081c4f4e5038c11383faae09d82a139..bb338177506373cde8e2e93a1bc9ee89f16912cb 100644 --- a/restool_faq.json +++ b/restool_faq.json @@ -8,6 +8,15 @@ "code": 11201001, "cn": "https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs/faqs-compiling-and-building-181", "en": "https://developer.huawei.com/consumer/en/doc/harmonyos-faqs/faqs-compiling-and-building-181" + }, + { + "code": 11211120, + "extSolutions": [ + { + "fileName": "module.json", + "solution": "Check whether the hvigorfile.ts in the project root directory or the module.json5 file in the module directory references undefined resources." + } + ] } ] } \ No newline at end of file diff --git a/src/restool_errors.cpp b/src/restool_errors.cpp index bcb572c10d6f5ffe293021c90e39c9fe71a29dfd..2a9aa3491fac29b3f415633ee3c835f76483620d 100644 --- a/src/restool_errors.cpp +++ b/src/restool_errors.cpp @@ -537,8 +537,8 @@ const std::string LOCALE_CMD_LINUX = "locale"; const std::string LOCALE_CMD_MAC = "defaults read -globalDomain AppleLocale"; const std::string LOCALE_CN = "zh_CN"; -std::map faqInfos; -MoreInfo defaultMoreInfo = {}; +std::map faqInfos; +FaqInfo defaultMoreInfo = {}; Language osLanguage = Language::EN; bool IsValidCmd(const std::string &cmd) @@ -733,7 +733,26 @@ Language GetOsLanguage() return Language::CN; } -void GetMoreInfo(cJSON *node, MoreInfo &info) +void GetExtSolutions(cJSON *node, FaqInfo &info) +{ + if (!node || !cJSON_IsArray(node)) { + return; + } + for (cJSON *subNode = node->child; subNode; subNode = node->next) { + ExtSolution solution{}; + cJSON *fileNameNode = cJSON_GetObjectItem(subNode, "fileName"); + if (fileNameNode && cJSON_IsString(fileNameNode)) { + solution.fileName = fileNameNode->valuestring; + } + cJSON *solutionNode = cJSON_GetObjectItem(subNode, "solution"); + if (solutionNode && cJSON_IsString(solutionNode)) { + solution.solution = solutionNode->valuestring; + } + info.extSolutions.push_back(solution); + } +} + +void GetFaqInfo(cJSON *node, FaqInfo &info) { if (node && cJSON_IsObject(node)) { cJSON *cn = cJSON_GetObjectItem(node, "cn"); @@ -744,6 +763,7 @@ void GetMoreInfo(cJSON *node, MoreInfo &info) if (en && cJSON_IsString(en)) { info.en = en->valuestring; } + GetExtSolutions(cJSON_GetObjectItem(node, "extSolutions"), info); } } @@ -760,20 +780,20 @@ void InitFaq(const std::string &restoolPath) return; } cJSON *defaultNode = cJSON_GetObjectItem(root, "default"); - GetMoreInfo(defaultNode, defaultMoreInfo); + GetFaqInfo(defaultNode, defaultMoreInfo); cJSON *faqsNode = cJSON_GetObjectItem(root, "faqs"); if (!faqsNode || !cJSON_IsArray(faqsNode) || cJSON_GetArraySize(faqsNode) == 0) { cJSON_Delete(root); return; } - for (cJSON *infoNode = faqsNode->child; infoNode; infoNode = faqsNode->next) { + for (cJSON *infoNode = faqsNode->child; infoNode; infoNode = infoNode->next) { cJSON *codeNode = cJSON_GetObjectItem(infoNode, "code"); if (!codeNode || !cJSON_IsNumber(codeNode)) { continue; } uint32_t code = static_cast(codeNode->valueint); - MoreInfo info = {}; - GetMoreInfo(infoNode, info); + FaqInfo info = {}; + GetFaqInfo(infoNode, info); faqInfos[code] = info; } cJSON_Delete(root); @@ -788,7 +808,7 @@ ErrorInfo GetError(const uint32_t &errCode) } auto faq = faqInfos.find(errCode); if (faq != faqInfos.end()) { - error.moreInfo_ = faq->second; + error.moreInfo_ = { faq->second.cn, faq->second.en }; } return error; } @@ -808,27 +828,38 @@ void PrintError(const ErrorInfo &error) errMsg.append(" At file: ").append(error.position_); } errMsg.append("\n"); - if (!error.solutions_.empty()) { - errMsg.append("* Try the following:").append("\n"); - for (const auto &solution : error.solutions_) { errMsg.append(" > ").append(solution).append("\n"); } - std::string moreInfo; - if (osLanguage == Language::CN) { - if (!error.moreInfo_.cn.empty()) { - moreInfo = error.moreInfo_.cn; - } else { - moreInfo = defaultMoreInfo.cn; + if (error.solutions_.empty()) { + std::cerr << errMsg; + return; + } + errMsg.append("* Try the following:").append("\n"); + for (const auto &solution : error.solutions_) { errMsg.append(" > ").append(solution).append("\n"); } + auto faq = faqInfos.find(error.code_); + if (faq != faqInfos.end()) { + std::vector extSolutions = faq->second.extSolutions; + for (const auto &solution : extSolutions) { + if (FileEntry(error.position_).GetFilePath().GetFilename() == solution.fileName) { + errMsg.append(" > ").append(solution.solution).append("\n"); } + } + } + std::string moreInfo; + if (osLanguage == Language::CN) { + if (!error.moreInfo_.cn.empty()) { + moreInfo = error.moreInfo_.cn; } else { - if (!error.moreInfo_.en.empty()) { - moreInfo = error.moreInfo_.en; - } else { - moreInfo = defaultMoreInfo.en; - } + moreInfo = defaultMoreInfo.cn; } - if (!moreInfo.empty()) { - errMsg.append("> More info: ").append(moreInfo).append("\n"); + } else { + if (!error.moreInfo_.en.empty()) { + moreInfo = error.moreInfo_.en; + } else { + moreInfo = defaultMoreInfo.en; } } + if (!moreInfo.empty()) { + errMsg.append(" > More info: ").append(moreInfo).append("\n"); + } std::cerr << errMsg; } } // namespace Restool