From 436634c5e4c568f5512b03fff488f03a71ba5bd1 Mon Sep 17 00:00:00 2001 From: LY Date: Wed, 29 May 2024 20:30:02 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E7=A7=BB=E9=99=A4napi=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LY --- interfaces/js/kits/src/i18n_addon.cpp | 44 +++++++++---------- .../js/kits/src/i18n_calendar_addon.cpp | 40 ++++++++--------- interfaces/js/kits/src/i18n_system_addon.cpp | 34 +++----------- .../js/kits/src/i18n_timezone_addon.cpp | 2 +- interfaces/js/kits/src/i18n_unicode_addon.cpp | 36 +++++++-------- interfaces/js/kits/src/intl_addon.cpp | 6 +-- interfaces/js/kits/src/variable_convertor.cpp | 2 +- 7 files changed, 69 insertions(+), 95 deletions(-) diff --git a/interfaces/js/kits/src/i18n_addon.cpp b/interfaces/js/kits/src/i18n_addon.cpp index a3376fd4..ca258655 100644 --- a/interfaces/js/kits/src/i18n_addon.cpp +++ b/interfaces/js/kits/src/i18n_addon.cpp @@ -352,7 +352,7 @@ LocaleInfo* ProcessJsParamLocale(napi_env env, napi_value argv) int32_t code = 0; std::string localeTag = VariableConvertor::GetString(env, argv, code); if (code != 0) { - napi_throw_type_error(env, nullptr, "ProcessJsParamLocale: Failed to obtain the parameter."); + HILOG_ERROR_I18N("ProcessJsParamLocale: Failed to obtain the parameter."); return nullptr; } UErrorCode icuStatus = U_ZERO_ERROR; @@ -369,7 +369,7 @@ bool ProcessJsParamLocaleList(napi_env env, napi_value argv, std::vector localeTagList; if (!VariableConvertor::GetStringArrayFromJsParam(env, argv, "localeList", localeTagList)) { - napi_throw_type_error(env, nullptr, "ProcessJsParamLocaleList: Failed to obtain the parameter."); + HILOG_ERROR_I18N("ProcessJsParamLocaleList: Failed to obtain the parameter."); return false; } if (localeTagList.size() == 0) { @@ -467,7 +467,7 @@ napi_value I18nAddon::GetThreeLetterLanguage(napi_env env, napi_callback_info in int32_t code = 0; std::string languageTag = VariableConvertor::GetString(env, argv[0], code); if (code != 0) { - napi_throw_type_error(env, nullptr, "GetThreeLetterLanguage: Failed to obtain the parameter."); + HILOG_ERROR_I18N("GetThreeLetterLanguage: Failed to obtain the parameter."); return nullptr; } @@ -477,7 +477,7 @@ napi_value I18nAddon::GetThreeLetterLanguage(napi_env env, napi_callback_info in status = napi_create_string_utf8(env, language.c_str(), NAPI_AUTO_LENGTH, &result); if (status != napi_ok || language == "") { HILOG_ERROR_I18N("GetThreeLetterLanguage create string fail or empty"); - ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "a valid locale", true); + ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "locale", "a valid locale", true); return nullptr; } return result; @@ -491,7 +491,7 @@ napi_value I18nAddon::GetThreeLetterRegion(napi_env env, napi_callback_info info void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); if (status != napi_ok) { - napi_throw_type_error(env, nullptr, "GetThreeLetterRegion: Failed to obtain the parameter."); + HILOG_ERROR_I18N("GetThreeLetterRegion: Failed to obtain the parameter."); return nullptr; } else if (argc < 1) { ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "", true); @@ -508,7 +508,7 @@ napi_value I18nAddon::GetThreeLetterRegion(napi_env env, napi_callback_info info int32_t code = 0; std::string regionTag = VariableConvertor::GetString(env, argv[0], code); if (code != 0) { - napi_throw_type_error(env, nullptr, "GetThreeLetterRegion: Failed to obtain the parameter."); + HILOG_ERROR_I18N("GetThreeLetterRegion: Failed to obtain the parameter."); return nullptr; } @@ -615,7 +615,7 @@ napi_value I18nAddon::I18nTransliteratorConstructor(napi_env env, napi_callback_ napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "I18nTransliteratorConstructor: Parameter type does not match"); + HILOG_ERROR_I18N("I18nTransliteratorConstructor: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -670,7 +670,7 @@ napi_value I18nAddon::Transform(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Transform: Parameter type does not match"); return nullptr; } size_t len = 0; @@ -826,13 +826,13 @@ napi_value I18nAddon::PhoneNumberFormatConstructor(napi_env env, napi_callback_i napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("PhoneNumberFormatConstructor: Parameter type does not match"); return nullptr; } size_t len = 0; status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); if (status != napi_ok) { - HILOG_ERROR_I18N("Get country tag length failed"); + HILOG_ERROR_I18N("PhoneNumberFormatConstructor: Get country tag length failed"); return nullptr; } std::vector country (len + 1); @@ -885,7 +885,7 @@ napi_value I18nAddon::IsValidPhoneNumber(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsValidPhoneNumber: Parameter type does not match"); return nullptr; } @@ -967,7 +967,7 @@ napi_value I18nAddon::FormatPhoneNumber(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("FormatPhoneNumber: Parameter type does not match"); return nullptr; } @@ -1057,7 +1057,7 @@ napi_value I18nAddon::I18nBreakIteratorConstructor(napi_env env, napi_callback_i napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("BreakIteratorConstructor: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -1269,12 +1269,12 @@ napi_value I18nAddon::Next(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Next: Parameter type does not match"); return nullptr; } status = napi_get_value_int32(env, argv[0], &value); if (status != napi_ok) { - HILOG_ERROR_I18N("Retrieve next value failed"); + HILOG_ERROR_I18N("Next: Retrieve next value failed"); return nullptr; } } @@ -1307,7 +1307,7 @@ napi_value I18nAddon::SetText(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("SetText: Parameter type does not match"); return nullptr; } size_t len = 0; @@ -1369,7 +1369,7 @@ napi_value I18nAddon::Following(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Following: Parameter type does not match"); return nullptr; } int value; @@ -1408,7 +1408,7 @@ napi_value I18nAddon::IsBoundary(napi_env env, napi_callback_info info) int value; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsBoundary: Parameter type does not match"); return nullptr; } status = napi_get_value_int32(env, argv[0], &value); @@ -1441,7 +1441,7 @@ napi_value I18nAddon::I18nIndexUtilConstructor(napi_env env, napi_callback_info napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IndexUtilConstructor: Parameter type does not match"); return nullptr; } size_t len = 0; @@ -1558,7 +1558,7 @@ napi_value I18nAddon::AddLocale(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("AddLocale: Parameter type does not match"); return nullptr; } size_t len = 0; @@ -1593,13 +1593,13 @@ napi_value I18nAddon::GetIndex(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("GetIndex: Parameter type does not match"); return nullptr; } size_t len = 0; napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); if (status != napi_ok) { - HILOG_ERROR_I18N("Get String length failed"); + HILOG_ERROR_I18N("GetIndex: Get String length failed"); return nullptr; } std::vector buf(len + 1); diff --git a/interfaces/js/kits/src/i18n_calendar_addon.cpp b/interfaces/js/kits/src/i18n_calendar_addon.cpp index 6cef1fc2..56919cab 100644 --- a/interfaces/js/kits/src/i18n_calendar_addon.cpp +++ b/interfaces/js/kits/src/i18n_calendar_addon.cpp @@ -188,7 +188,7 @@ napi_value I18nCalendarAddon::I18nCalendarConstructor(napi_env env, napi_callbac napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("CalendarConstructor: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -263,12 +263,12 @@ napi_value I18nCalendarAddon::Set(napi_env env, napi_callback_info info) for (int i = 0; i < 3; ++i) { // There are at least 3 arguments. napi_typeof(env, argv[i], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Set: Parameter type does not match"); return nullptr; } status = napi_get_value_int32(env, argv[i], times + i); if (status != napi_ok) { - HILOG_ERROR_I18N("Retrieve time value failed"); + HILOG_ERROR_I18N("Set: Retrieve time value failed"); return nullptr; } } @@ -319,19 +319,19 @@ napi_value I18nCalendarAddon::SetTimeZone(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("SetTimeZone: Parameter type does not match"); return nullptr; } size_t len = 0; napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); if (status != napi_ok) { - HILOG_ERROR_I18N("Get timezone length failed"); + HILOG_ERROR_I18N("SetTimeZone: Get timezone length failed"); return nullptr; } std::vector buf(len + 1); status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); if (status != napi_ok) { - HILOG_ERROR_I18N("Get timezone failed"); + HILOG_ERROR_I18N("SetTimeZone: Get timezone failed"); return nullptr; } std::string timezone(buf.data()); @@ -379,7 +379,7 @@ napi_value I18nCalendarAddon::SetFirstDayOfWeek(napi_env env, napi_callback_info napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("SetFirstDayOfWeek: Parameter type does not match"); return nullptr; } int32_t value = 0; @@ -432,7 +432,7 @@ napi_value I18nCalendarAddon::SetMinimalDaysInFirstWeek(napi_env env, napi_callb napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("SetMinimalDaysInFirstWeek: Parameter type does not match"); return nullptr; } int32_t value = 0; @@ -462,7 +462,7 @@ napi_value I18nCalendarAddon::Get(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Get: Parameter type does not match"); return nullptr; } size_t len = 0; @@ -506,8 +506,7 @@ napi_value I18nCalendarAddon::Add(napi_env env, napi_callback_info info) void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); if (status != napi_ok) { - HILOG_ERROR_I18N("can not obtain add function param."); - napi_throw_type_error(env, nullptr, "I18nCalendarAddon::Add: Failed to obtain the parameter."); + HILOG_ERROR_I18N("Add: can not obtain add function param."); return nullptr; } napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -531,8 +530,7 @@ napi_value I18nCalendarAddon::Add(napi_env env, napi_callback_info info) int32_t amount; status = napi_get_value_int32(env, argv[1], &amount); if (status != napi_ok) { - HILOG_ERROR_I18N("Can not obtain add function param."); - napi_throw_type_error(env, nullptr, "I18nCalendarAddon::Add: Failed to obtain the amount."); + HILOG_ERROR_I18N("Add: Can not obtain add function param."); return nullptr; } I18nCalendarAddon *obj = nullptr; @@ -556,7 +554,7 @@ napi_value I18nCalendarAddon::GetDisplayName(napi_env env, napi_callback_info in napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("GetDisplayName: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -671,8 +669,7 @@ napi_value I18nCalendarAddon::CompareDays(napi_env env, napi_callback_info info) UDate milliseconds = 0; napi_status status = napi_get_date_value(env, argv[0], &milliseconds); if (status != napi_ok) { - HILOG_ERROR_I18N("compareDays function param is not Date"); - napi_throw_type_error(env, nullptr, "CompareDays: Failed to obtain the milliseconds"); + HILOG_ERROR_I18N("compareDays: function param is not Date"); return nullptr; } @@ -703,7 +700,7 @@ CalendarType I18nCalendarAddon::GetCalendarType(napi_env env, napi_value value) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, value, &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("GetCalendarType: Parameter type does not match"); return type; } int32_t code = 0; @@ -727,12 +724,12 @@ void I18nCalendarAddon::SetField(napi_env env, napi_value value, UCalendarDateFi napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, value, &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("SetField: Parameter type does not match"); return; } napi_status status = napi_get_value_int32(env, value, &val); if (status != napi_ok) { - HILOG_ERROR_I18N("Retrieve field failed"); + HILOG_ERROR_I18N("SetField: Retrieve field failed"); return; } if (calendar_ != nullptr) { @@ -744,8 +741,7 @@ std::string I18nCalendarAddon::GetAddField(napi_env &env, napi_value &value, int { std::string field = VariableConvertor::GetString(env, value, code); if (code != 0) { - HILOG_ERROR_I18N("can't get string from js array param."); - napi_throw_type_error(env, nullptr, "GetAddField: Failed to obtain the field."); + HILOG_ERROR_I18N("GetAddField: can't get string from js array param."); return field; } if (g_fieldsInFunctionAdd.find(field) == g_fieldsInFunctionAdd.end()) { @@ -786,7 +782,7 @@ void I18nCalendarAddon::SetMilliseconds(napi_env env, napi_value value) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, value, &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("SetMilliseconds: Parameter type does not match"); return; } napi_status status = napi_get_value_double(env, value, &milliseconds); diff --git a/interfaces/js/kits/src/i18n_system_addon.cpp b/interfaces/js/kits/src/i18n_system_addon.cpp index 8b6f0f75..8ede87f5 100644 --- a/interfaces/js/kits/src/i18n_system_addon.cpp +++ b/interfaces/js/kits/src/i18n_system_addon.cpp @@ -412,7 +412,7 @@ napi_value I18nSystemAddon::GetDisplayCountryImpl(napi_env env, napi_callback_in } std::string value = LocaleConfig::GetDisplayRegion(localeBuf.data(), displayLocaleBuf.data(), sentenceCase); if (value.length() == 0) { - napi_throw_type_error(env, nullptr, "getDisplayCountry: result is empty."); + HILOG_ERROR_I18N("GetDisplayCountryImpl: result is empty."); } napi_value result = nullptr; status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result); @@ -429,8 +429,6 @@ bool I18nSystemAddon::GetCountryNapiValueWithError(napi_env env, napi_value napi napi_status status = napi_get_value_string_utf8(env, napiValue, valueBuf, len + 1, &len); if (status != napi_ok) { HILOG_ERROR_I18N("GetDisplayCountryImpl: Failed to get string item argv[%{public}s]", index.c_str()); - std::string message = "getDisplayCountry: Failed to get string item argv[" + index + "]."; - napi_throw_type_error(env, nullptr, message.c_str()); return false; } return true; @@ -473,8 +471,7 @@ napi_value I18nSystemAddon::GetDisplayLanguageImpl(napi_env env, napi_callback_i } std::string value = LocaleConfig::GetDisplayLanguage(localeBuf.data(), displayLocaleBuf.data(), sentenceCase); if (value.length() == 0) { - std::string message = "getDisplayLanguage: result is empty."; - napi_throw_type_error(env, nullptr, message.c_str()); + HILOG_ERROR_I18N("GetDisplayLanguageImpl: result is empty."); return nullptr; } napi_value result = nullptr; @@ -492,8 +489,6 @@ bool I18nSystemAddon::GetNapiStringValueWithError(napi_env env, napi_value napiV napi_status status = napi_get_value_string_utf8(env, napiValue, valueBuf, len + 1, &len); if (status != napi_ok) { HILOG_ERROR_I18N("GetDisplayLanguageImpl: Failed to get string item argv[%{public}s]", index.c_str()); - std::string message = "getDisplayLanguage: Failed to get string item argv[" + index + "]."; - napi_throw_type_error(env, nullptr, message.c_str()); return false; } return true; @@ -518,8 +513,7 @@ napi_value I18nSystemAddon::GetSystemCountriesImpl(napi_env env, napi_callback_i std::vector localeBuf(len + 1); status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len); if (status != napi_ok) { - std::string message = "getSystemCountries: Failed to get string item."; - napi_throw_type_error(env, nullptr, message.c_str()); + HILOG_ERROR_I18N("GetSystemCountriesImpl: Failed to get string item."); return nullptr; } std::vector systemCountries; @@ -566,8 +560,7 @@ napi_value I18nSystemAddon::IsSuggestedImpl(napi_env env, napi_callback_info inf std::vector languageBuf(len + 1); status = napi_get_value_string_utf8(env, argv[0], languageBuf.data(), len + 1, &len); if (status != napi_ok) { - std::string message = "isSuggested: Failed to get string item argv[0]"; - napi_throw_type_error(env, nullptr, message.c_str()); + HILOG_ERROR_I18N("isSuggested: Failed to get string item argv[0]"); return nullptr; } bool isSuggested = false; @@ -576,8 +569,7 @@ napi_value I18nSystemAddon::IsSuggestedImpl(napi_env env, napi_callback_info inf std::vector regionBuf(len + 1); status = napi_get_value_string_utf8(env, argv[1], regionBuf.data(), len + 1, &len); if (status != napi_ok) { - std::string message = "isSuggested: Failed to get string item argv[1]"; - napi_throw_type_error(env, nullptr, message.c_str()); + HILOG_ERROR_I18N("isSuggested: Failed to get string item argv[1]"); return nullptr; } isSuggested = LocaleConfig::IsSuggested(languageBuf.data(), regionBuf.data()); @@ -614,8 +606,6 @@ napi_value I18nSystemAddon::SetSystemLanguageImpl(napi_env env, napi_callback_in status = napi_get_value_string_utf8(env, argv[0], languageBuf.data(), len + 1, &len); if (status != napi_ok) { HILOG_ERROR_I18N("SetSystemLanguageImpl: Failed to get string item"); - std::string message = "setSystemLanguage: Failed to get string item."; - napi_throw_type_error(env, nullptr, message.c_str()); return nullptr; } I18nErrorCode err = I18nServiceAbilityClient::SetSystemLanguage(languageBuf.data()); @@ -657,8 +647,6 @@ napi_value I18nSystemAddon::SetSystemRegionImpl(napi_env env, napi_callback_info status = napi_get_value_string_utf8(env, argv[0], regionBuf.data(), len + 1, &len); if (status != napi_ok) { HILOG_ERROR_I18N("SetSystemRegionImpl: Failed to get string item"); - std::string message = "setSystemRegion: Failed to get string item."; - napi_throw_type_error(env, nullptr, message.c_str()); return nullptr; } I18nErrorCode err = I18nServiceAbilityClient::SetSystemRegion(regionBuf.data()); @@ -700,8 +688,6 @@ napi_value I18nSystemAddon::SetSystemLocaleImpl(napi_env env, napi_callback_info status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len); if (status != napi_ok) { HILOG_ERROR_I18N("SetSystemLocaleImpl: Failed to get string item"); - std::string message = "setSystemLocale: Failed to get string item."; - napi_throw_type_error(env, nullptr, message.c_str()); return nullptr; } I18nErrorCode err = I18nServiceAbilityClient::SetSystemLocale(localeBuf.data()); @@ -741,9 +727,7 @@ napi_value I18nSystemAddon::Set24HourClockImpl(napi_env env, napi_callback_info bool option = false; status = napi_get_value_bool(env, argv[0], &option); if (status != napi_ok) { - HILOG_ERROR_I18N("Failed to get boolean item"); - std::string message = "set24HourClock: Failed to get boolean item."; - napi_throw_type_error(env, nullptr, message.c_str()); + HILOG_ERROR_I18N("Set24HourClockImpl: Failed to get boolean item"); return nullptr; } std::string optionStr = option ? "true" : "false"; @@ -786,8 +770,6 @@ napi_value I18nSystemAddon::AddPreferredLanguageImpl(napi_env env, napi_callback status = napi_get_value_int32(env, argv[1], &index); if (status != napi_ok) { HILOG_ERROR_I18N("addPreferrdLanguage: get index failed"); - std::string message = "addPreferrdLanguage: get index failed."; - napi_throw_type_error(env, nullptr, message.c_str()); return nullptr; } } @@ -842,8 +824,6 @@ napi_value I18nSystemAddon::RemovePreferredLanguageImpl(napi_env env, napi_callb status = napi_get_value_int32(env, argv[0], &index); if (status != napi_ok) { HILOG_ERROR_I18N("removePreferrdLanguage: get index failed"); - std::string message = "removePreferrdLanguage: get index failed."; - napi_throw_type_error(env, nullptr, message.c_str()); return nullptr; } len = static_cast(PreferredLanguage::GetPreferredLanguageList().size()); @@ -931,8 +911,6 @@ bool I18nSystemAddon::ParseStringParam(napi_env env, napi_value argv, bool throw napi_status status = napi_get_value_string_utf8(env, argv, nullptr, 0, &len); if (status != napi_ok) { HILOG_ERROR_I18N("get string parameter length failed"); - std::string message = "get string parameter length failed."; - napi_throw_type_error(env, nullptr, message.c_str()); return false; } std::vector res(len + 1); diff --git a/interfaces/js/kits/src/i18n_timezone_addon.cpp b/interfaces/js/kits/src/i18n_timezone_addon.cpp index e8425428..3058f0ef 100644 --- a/interfaces/js/kits/src/i18n_timezone_addon.cpp +++ b/interfaces/js/kits/src/i18n_timezone_addon.cpp @@ -378,7 +378,7 @@ napi_value I18nTimeZoneAddon::GetTimeZoneDisplayName(napi_env env, napi_callback std::string result; if (parameterStatus == -1) { // -1 represents Invalid parameter. - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("GetTimeZoneDisplayName: Parameter type does not match"); return nullptr; } else if (parameterStatus == 0) { result = obj->timezone_->GetDisplayName(); diff --git a/interfaces/js/kits/src/i18n_unicode_addon.cpp b/interfaces/js/kits/src/i18n_unicode_addon.cpp index e948848b..378087cc 100644 --- a/interfaces/js/kits/src/i18n_unicode_addon.cpp +++ b/interfaces/js/kits/src/i18n_unicode_addon.cpp @@ -108,7 +108,7 @@ napi_value I18nUnicodeAddon::IsDigitAddon(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsDigitAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -120,7 +120,7 @@ napi_value I18nUnicodeAddon::IsDigitAddon(napi_env env, napi_callback_info info) napi_value result = nullptr; status = napi_get_boolean(env, isDigit, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isDigit boolean value failed"); + HILOG_ERROR_I18N("IsDigitAddon: Create isDigit boolean value failed"); return nullptr; } return result; @@ -139,7 +139,7 @@ napi_value I18nUnicodeAddon::IsSpaceCharAddon(napi_env env, napi_callback_info i napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsSpaceCharAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -151,7 +151,7 @@ napi_value I18nUnicodeAddon::IsSpaceCharAddon(napi_env env, napi_callback_info i napi_value result = nullptr; status = napi_get_boolean(env, isSpaceChar, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isSpaceChar boolean value failed"); + HILOG_ERROR_I18N("IsSpaceCharAddon: Create boolean value failed"); return nullptr; } return result; @@ -170,7 +170,7 @@ napi_value I18nUnicodeAddon::IsWhiteSpaceAddon(napi_env env, napi_callback_info napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsWhiteSpaceAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -182,7 +182,7 @@ napi_value I18nUnicodeAddon::IsWhiteSpaceAddon(napi_env env, napi_callback_info napi_value result = nullptr; status = napi_get_boolean(env, isWhiteSpace, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isWhiteSpace boolean value failed"); + HILOG_ERROR_I18N("IsWhiteSpaceAddon: Create boolean value failed"); return nullptr; } return result; @@ -201,7 +201,7 @@ napi_value I18nUnicodeAddon::IsRTLCharacterAddon(napi_env env, napi_callback_inf napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsRTLCharacterAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -213,7 +213,7 @@ napi_value I18nUnicodeAddon::IsRTLCharacterAddon(napi_env env, napi_callback_inf napi_value result = nullptr; status = napi_get_boolean(env, isRTLCharacter, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isRTLCharacter boolean value failed"); + HILOG_ERROR_I18N("IsRTLCharacterAddon: Create boolean value failed"); return nullptr; } return result; @@ -232,7 +232,7 @@ napi_value I18nUnicodeAddon::IsIdeoGraphicAddon(napi_env env, napi_callback_info napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsIdeoGraphicAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -244,7 +244,7 @@ napi_value I18nUnicodeAddon::IsIdeoGraphicAddon(napi_env env, napi_callback_info napi_value result = nullptr; status = napi_get_boolean(env, isIdeoGraphic, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isIdeoGraphic boolean value failed"); + HILOG_ERROR_I18N("IsIdeoGraphicAddon: Create boolean value failed"); return nullptr; } return result; @@ -263,7 +263,7 @@ napi_value I18nUnicodeAddon::IsLetterAddon(napi_env env, napi_callback_info info napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsLetterAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -275,7 +275,7 @@ napi_value I18nUnicodeAddon::IsLetterAddon(napi_env env, napi_callback_info info napi_value result = nullptr; status = napi_get_boolean(env, isLetter, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isLetter boolean value failed"); + HILOG_ERROR_I18N("IsLetterAddon: Create boolean value failed"); return nullptr; } return result; @@ -294,7 +294,7 @@ napi_value I18nUnicodeAddon::IsLowerCaseAddon(napi_env env, napi_callback_info i napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsLowerCaseAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -306,7 +306,7 @@ napi_value I18nUnicodeAddon::IsLowerCaseAddon(napi_env env, napi_callback_info i napi_value result = nullptr; status = napi_get_boolean(env, isLowerCase, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isLowerCase boolean value failed"); + HILOG_ERROR_I18N("IsLowerCaseAddon: Create isLowerCase boolean value failed"); return nullptr; } return result; @@ -325,7 +325,7 @@ napi_value I18nUnicodeAddon::IsUpperCaseAddon(napi_env env, napi_callback_info i napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("IsUpperCaseAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -337,7 +337,7 @@ napi_value I18nUnicodeAddon::IsUpperCaseAddon(napi_env env, napi_callback_info i napi_value result = nullptr; status = napi_get_boolean(env, isUpperCase, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create isUpperCase boolean value failed"); + HILOG_ERROR_I18N("IsUpperCaseAddon: Create boolean value failed"); return nullptr; } return result; @@ -356,7 +356,7 @@ napi_value I18nUnicodeAddon::GetTypeAddon(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("GetTypeAddon: Parameter type does not match"); return nullptr; } int32_t code = 0; @@ -368,7 +368,7 @@ napi_value I18nUnicodeAddon::GetTypeAddon(napi_env env, napi_callback_info info) napi_value result = nullptr; status = napi_create_string_utf8(env, type.c_str(), NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { - HILOG_ERROR_I18N("Create getType string value failed"); + HILOG_ERROR_I18N("GetTypeAddon: Create getType string value failed"); return nullptr; } return result; diff --git a/interfaces/js/kits/src/intl_addon.cpp b/interfaces/js/kits/src/intl_addon.cpp index f16aaa37..606481ba 100644 --- a/interfaces/js/kits/src/intl_addon.cpp +++ b/interfaces/js/kits/src/intl_addon.cpp @@ -282,7 +282,7 @@ std::string GetLocaleTag(napi_env env, napi_value argv) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv, &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("GetLocaleTag: Parameter type does not match"); return ""; } size_t len = 0; @@ -1377,7 +1377,7 @@ bool GetStringParameter(napi_env env, napi_value value, std::vector &buf) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, value, &valueType); if (valueType != napi_valuetype::napi_string) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Parameter type does not match"); return false; } size_t len = 0; @@ -1760,7 +1760,7 @@ napi_value IntlAddon::Select(napi_env env, napi_callback_info info) napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_number) { - napi_throw_type_error(env, nullptr, "Parameter type does not match"); + HILOG_ERROR_I18N("Select: Parameter type does not match"); return nullptr; } diff --git a/interfaces/js/kits/src/variable_convertor.cpp b/interfaces/js/kits/src/variable_convertor.cpp index 4b8c6a5f..9cf0b58e 100644 --- a/interfaces/js/kits/src/variable_convertor.cpp +++ b/interfaces/js/kits/src/variable_convertor.cpp @@ -129,7 +129,7 @@ bool VariableConvertor::GetStringArrayFromJsParam(napi_env env, napi_value &jsAr napi_get_element(env, jsArray, i, &element); std::string str = GetString(env, element, code); if (code != 0) { - napi_throw_type_error(env, nullptr, "GetStringArrayFromJsParam: Failed to obtain the parameter."); + HILOG_ERROR_I18N("GetStringArrayFromJsParam: Failed to obtain the parameter."); return false; } strArray.push_back(str); -- Gitee From 45c15cb3ed42694d2b629620cc9d5eede9de6362 Mon Sep 17 00:00:00 2001 From: LY Date: Tue, 4 Jun 2024 01:45:15 +0000 Subject: [PATCH 2/5] update frameworks/intl/src/date_rule_init.cpp. Signed-off-by: LY --- frameworks/intl/src/date_rule_init.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/frameworks/intl/src/date_rule_init.cpp b/frameworks/intl/src/date_rule_init.cpp index 9b11f577..8e802daa 100644 --- a/frameworks/intl/src/date_rule_init.cpp +++ b/frameworks/intl/src/date_rule_init.cpp @@ -23,6 +23,7 @@ DateRuleInit::DateRuleInit(std::string& locale) dateTimeRule = new DateTimeRule(locale); if (dateTimeRule == nullptr) { HILOG_ERROR_I18N("DateTimeRule construct failed."); + return; } this->locale = dateTimeRule->GetLocale(); filter = new DateTimeFilter(this->locale, dateTimeRule); @@ -34,8 +35,12 @@ DateRuleInit::DateRuleInit(std::string& locale) DateRuleInit::~DateRuleInit() { - delete dateTimeRule; - delete filter; + if (dateTimeRule != nullptr) { + delete dateTimeRule; + } + if (filter != nullptr) { + delete filter; + } } std::vector DateRuleInit::Detect(icu::UnicodeString& message) @@ -43,8 +48,10 @@ std::vector DateRuleInit::Detect(icu::UnicodeString& messag std::vector matches = GetMatches(message); std::vector clearMatches = ClearFind(message); std::vector pastMatches = PastFind(message); - // Filter results that don't meet rules - matches = filter->Filter(message, matches, clearMatches, pastMatches); + if (filter != nullptr) { + // Filter results that don't meet rules + matches = filter->Filter(message, matches, clearMatches, pastMatches); + } return matches; } -- Gitee From 2c1ebc80ff23220289271b7592cc7290409ece15 Mon Sep 17 00:00:00 2001 From: LY Date: Tue, 4 Jun 2024 01:51:01 +0000 Subject: [PATCH 3/5] update frameworks/intl/src/date_time_filter.cpp. Signed-off-by: LY --- frameworks/intl/src/date_time_filter.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/frameworks/intl/src/date_time_filter.cpp b/frameworks/intl/src/date_time_filter.cpp index 57db0a1b..ccc0259a 100644 --- a/frameworks/intl/src/date_time_filter.cpp +++ b/frameworks/intl/src/date_time_filter.cpp @@ -303,6 +303,10 @@ std::vector DateTimeFilter::FilterDateTime(icu::UnicodeStri icu::UnicodeString joiner = content.tempSubString(lastMatch.GetEnd(), match.GetBegin() - lastMatch.GetEnd()); icu::RegexMatcher* matcher = pattern->matcher(joiner, status); + if (matcher == nullptr) { + HILOG_ERROR_I18N("FilterDateTime failed because pattern matcher failed."); + return matches; + } bool isJoiner = (joiner.trim().isEmpty()) ? true : matcher->matches(status); // the substring meets the rule, the two times are combined. if (isJoiner) { -- Gitee From f9c2049d23e63fe60ba07f9eb09866c3436314c0 Mon Sep 17 00:00:00 2001 From: LY Date: Tue, 4 Jun 2024 01:51:35 +0000 Subject: [PATCH 4/5] update frameworks/intl/src/date_time_matched.cpp. Signed-off-by: LY --- frameworks/intl/src/date_time_matched.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/frameworks/intl/src/date_time_matched.cpp b/frameworks/intl/src/date_time_matched.cpp index be595eae..a10162b2 100644 --- a/frameworks/intl/src/date_time_matched.cpp +++ b/frameworks/intl/src/date_time_matched.cpp @@ -28,7 +28,9 @@ DateTimeMatched::DateTimeMatched(std::string& locale) DateTimeMatched::~DateTimeMatched() { - delete dateRuleInit; + if (dateRuleInit != nullptr) { + delete dateRuleInit; + } } std::vector DateTimeMatched::GetMatchedDateTime(icu::UnicodeString& message) -- Gitee From efe4435c917c721d820f4af2dc8df33ff580623e Mon Sep 17 00:00:00 2001 From: LY Date: Tue, 4 Jun 2024 01:54:15 +0000 Subject: [PATCH 5/5] update frameworks/intl/src/entity_recognizer.cpp. Signed-off-by: LY --- frameworks/intl/src/entity_recognizer.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frameworks/intl/src/entity_recognizer.cpp b/frameworks/intl/src/entity_recognizer.cpp index e1281a79..ca5ce11d 100644 --- a/frameworks/intl/src/entity_recognizer.cpp +++ b/frameworks/intl/src/entity_recognizer.cpp @@ -34,8 +34,12 @@ EntityRecognizer::EntityRecognizer(icu::Locale& locale) EntityRecognizer::~EntityRecognizer() { - delete phoneNumberMatched; - delete dateTimeMatched; + if (phoneNumberMatched != nullptr) { + delete phoneNumberMatched; + } + if (dateTimeMatched != nullptr) { + delete dateTimeMatched; + } } std::vector> EntityRecognizer::FindEntityInfo(std::string& message) -- Gitee