1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "error_util.h"
16
17 #include <string>
18 #include <unordered_map>
19 #include "i18n_hilog.h"
20
21 namespace OHOS {
22 namespace Global {
23 namespace I18n {
24 static const std::unordered_map<int32_t, std::string> ErrorCodeToMsg {
25 {I18N_NO_PERMISSION,
26 "Permission verification failed. The application does not have the permission required to call the API."},
27 {I18N_NOT_SYSTEM_APP,
28 "Permission verification failed. A non-system application calls a system API."},
29 {I18N_NOT_VALID, "Invalid parameter"},
30 {I18N_NOT_FOUND, "Parameter error"},
31 {I18N_OPTION_NOT_VALID, "Invalid option name"}
32 };
33
NapiThrow(napi_env env,int32_t errCode,const std::string & valueName,const std::string & valueContent,bool throwError)34 void ErrorUtil::NapiThrow(napi_env env, int32_t errCode, const std::string& valueName,
35 const std::string& valueContent, bool throwError)
36 {
37 if (!throwError) {
38 return;
39 }
40 napi_value code = nullptr;
41 napi_status status = napi_create_string_latin1(env, std::to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &code);
42 if (status != napi_ok) {
43 HILOG_ERROR_I18N("ErrorUtil::NapiThrow: create string %{public}d failed", errCode);
44 return;
45 }
46 napi_value message = nullptr;
47 auto iter = ErrorCodeToMsg.find(errCode);
48 std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
49 std::string allErrMsg;
50
51 if (errCode == I18N_NO_PERMISSION || errCode == I18N_NOT_SYSTEM_APP) {
52 allErrMsg = errMsg;
53 } else if (errCode == I18N_NOT_VALID) {
54 allErrMsg = errMsg + ", the " + valueName + " must be " + valueContent + ".";
55 } else if (valueContent.length() == 0) {
56 allErrMsg = errMsg + ", the " + valueName + " cannot be empty.";
57 } else {
58 allErrMsg = errMsg + ", the type of " + valueName + " must be " + valueContent + ".";
59 }
60
61 status = napi_create_string_latin1(env, allErrMsg.c_str(), NAPI_AUTO_LENGTH, &message);
62 if (status != napi_ok) {
63 HILOG_ERROR_I18N("ErrorUtil::NapiThrow: create string %{public}s failed", allErrMsg.c_str());
64 return;
65 }
66 napi_value error = nullptr;
67 status = napi_create_error(env, code, message, &error);
68 if (status != napi_ok) {
69 HILOG_ERROR_I18N("ErrorUtil::NapiThrow: create error failed");
70 return;
71 }
72 napi_throw(env, error);
73 }
74
NapiNotFoundError(napi_env env,int32_t errCode,const std::string & valueName,bool throwError)75 void ErrorUtil::NapiNotFoundError(napi_env env, int32_t errCode, const std::string& valueName,
76 bool throwError)
77 {
78 NapiThrow(env, errCode, valueName, "", throwError);
79 }
80 } // namespace I18n
81 } // namespace Global
82 } // namespace OHOS