• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "i18n_hilog.h"
21 
22 namespace OHOS {
23 namespace Global {
24 namespace I18n {
25 static const std::unordered_map<int32_t, std::string> ErrorCodeToMsg {
26     {I18N_NO_PERMISSION,
27         "Permission verification failed. The application does not have the permission required to call the API."},
28     {I18N_NOT_SYSTEM_APP,
29         "Permission verification failed. A non-system application calls a system API."},
30     {I18N_NOT_VALID, "Invalid parameter"},
31     {I18N_NOT_VALID_NEW, "Invalid parameter"},
32     {I18N_NOT_FOUND, "Parameter error"},
33     {I18N_OPTION_NOT_VALID, "Invalid option name"}
34 };
35 
NapiThrow(napi_env env,int32_t errCode,const std::string & valueName,const std::string & valueContent,bool throwError)36 void ErrorUtil::NapiThrow(napi_env env, int32_t errCode, const std::string& valueName,
37     const std::string& valueContent, bool throwError)
38 {
39     if (!throwError) {
40         return;
41     }
42     napi_value code = nullptr;
43     napi_status status = napi_create_string_latin1(env, std::to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &code);
44     if (status != napi_ok) {
45         HILOG_ERROR_I18N("ErrorUtil::NapiThrow: create string %{public}d failed", errCode);
46         return;
47     }
48     napi_value message = nullptr;
49     auto iter = ErrorCodeToMsg.find(errCode);
50     std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
51     std::string allErrMsg;
52 
53     if (errCode == I18N_NO_PERMISSION || errCode == I18N_NOT_SYSTEM_APP) {
54         allErrMsg = errMsg;
55     } else if (errCode == I18N_NOT_VALID || errCode == I18N_NOT_VALID_NEW) {
56         allErrMsg = errMsg + ", the " + valueName + " must be " + valueContent + ".";
57     } else if (valueContent.length() == 0) {
58         allErrMsg = errMsg + ", the " + valueName + " cannot be empty.";
59     } else {
60         allErrMsg = errMsg + ", the type of " + valueName + " must be " + valueContent + ".";
61     }
62 
63     status = napi_create_string_latin1(env, allErrMsg.c_str(), NAPI_AUTO_LENGTH, &message);
64     if (status != napi_ok) {
65         HILOG_ERROR_I18N("ErrorUtil::NapiThrow: create string %{public}s failed", allErrMsg.c_str());
66         return;
67     }
68     napi_value error = nullptr;
69     status = napi_create_error(env, code, message, &error);
70     if (status != napi_ok) {
71         HILOG_ERROR_I18N("ErrorUtil::NapiThrow: create error failed");
72         return;
73     }
74     napi_throw(env, error);
75 }
76 
NapiNotFoundError(napi_env env,int32_t errCode,const std::string & valueName,bool throwError)77 void ErrorUtil::NapiNotFoundError(napi_env env, int32_t errCode, const std::string& valueName,
78     bool throwError)
79 {
80     NapiThrow(env, errCode, valueName, "", throwError);
81 }
82 
NapiThrowUndefined(napi_env env,const std::string & value)83 void ErrorUtil::NapiThrowUndefined(napi_env env, const std::string& value)
84 {
85     napi_value code = nullptr;
86     napi_status status = napi_create_string_latin1(env, "undefined", NAPI_AUTO_LENGTH, &code);
87     if (status != napi_ok) {
88         HILOG_ERROR_I18N("ErrorUtil::NapiThrowUndefined: create string undefined failed");
89         return;
90     }
91     napi_value message = nullptr;
92     status = napi_create_string_latin1(env, value.c_str(), NAPI_AUTO_LENGTH, &message);
93     if (status != napi_ok) {
94         HILOG_ERROR_I18N("ErrorUtil::NapiThrowUndefined: create string %{public}s failed", value.c_str());
95         return;
96     }
97     napi_value error = nullptr;
98     status = napi_create_error(env, code, message, &error);
99     if (status != napi_ok) {
100         HILOG_ERROR_I18N("ErrorUtil::NapiThrowUndefined: create error failed");
101         return;
102     }
103     napi_throw(env, error);
104 }
105 } // namespace I18n
106 } // namespace Global
107 } // namespace OHOS