• 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 namespace OHOS {
21 namespace Global {
22 namespace I18n {
23 static const std::unordered_map<int32_t, std::string> ErrorCodeToMsg {
24     {I18N_NO_PERMISSION, "The application does not have permission to call this function"},
25     {I18N_NOT_VALID, "Param value not valid"},
26     {I18N_NOT_FOUND, "Check param failed"},
27     {I18N_OPTION_NOT_VALID, "Invalid option name"}
28 };
29 
NapiThrow(napi_env env,int32_t errCode,bool throwError)30 void ErrorUtil::NapiThrow(napi_env env, int32_t errCode, bool throwError)
31 {
32     if (!throwError) {
33         return;
34     }
35     napi_value code = nullptr;
36     napi_create_string_latin1(env, std::to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &code);
37 
38     napi_value message = nullptr;
39     auto iter = ErrorCodeToMsg.find(errCode);
40     std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
41     napi_create_string_latin1(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
42 
43     napi_value error = nullptr;
44     napi_create_error(env, code, message, &error);
45     napi_throw(env, error);
46 }
47 
48 } // namespace I18n
49 } // namespace Global
50 } // namespace OHOS