• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 "napi_util.h"
16 
17 namespace OHOS {
18 namespace HiviewDFX {
19 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger-napi");
CreateErrorMessage(napi_env env,std::string msg)20 napi_value NapiUtil::CreateErrorMessage(napi_env env, std::string msg)
21 {
22     napi_value result = nullptr;
23     napi_value message = nullptr;
24     if (napi_create_string_utf8(env, msg.c_str(), msg.length(), &message) != napi_ok) {
25         HIVIEW_LOGE("failed to create string");
26         return nullptr;
27     }
28     napi_value codeValue = nullptr;
29     std::string errCode = std::to_string(-1);
30     if (napi_create_string_utf8(env, errCode.c_str(), errCode.length(), &codeValue) != napi_ok) {
31         HIVIEW_LOGE("failed to create string");
32         return nullptr;
33     }
34     if (napi_create_error(env, codeValue, message, &result) != napi_ok) {
35         HIVIEW_LOGE("failed to create error");
36         return nullptr;
37     }
38     return result;
39 }
40 
CreateUndefined(napi_env env)41 napi_value NapiUtil::CreateUndefined(napi_env env)
42 {
43     napi_value result = nullptr;
44     NAPI_CALL(env, napi_get_undefined(env, &result));
45     return result;
46 }
47 
SetPropertyInt32(napi_env env,napi_value object,std::string name,int32_t value)48 void NapiUtil::SetPropertyInt32(napi_env env, napi_value object, std::string name, int32_t value)
49 {
50     napi_value propertyValue = nullptr;
51     if (napi_create_int32(env, value, &propertyValue) != napi_ok) {
52         HIVIEW_LOGE("failed to create int32");
53         return;
54     }
55     napi_set_named_property(env, object, name.c_str(), propertyValue);
56 }
57 
SetPropertyInt64(napi_env env,napi_value object,std::string name,int64_t value)58 void NapiUtil::SetPropertyInt64(napi_env env, napi_value object, std::string name, int64_t value)
59 {
60     napi_value propertyValue = nullptr;
61     if (napi_create_int64(env, value, &propertyValue) != napi_ok) {
62         HIVIEW_LOGE("failed to create int64");
63         return;
64     }
65     napi_set_named_property(env, object, name.c_str(), propertyValue);
66 }
67 
SetPropertyStringUtf8(napi_env env,napi_value object,std::string name,std::string value)68 void NapiUtil::SetPropertyStringUtf8(napi_env env, napi_value object, std::string name, std::string value)
69 {
70     napi_value propertyValue = nullptr;
71     if (napi_create_string_utf8(env, value.c_str(), value.length(), &propertyValue) != napi_ok) {
72         HIVIEW_LOGE("failed to create string");
73         return;
74     }
75     napi_set_named_property(env, object, name.c_str(), propertyValue);
76 }
77 
IsMatchType(napi_env env,napi_value value,napi_valuetype type)78 bool NapiUtil::IsMatchType(napi_env env, napi_value value, napi_valuetype type)
79 {
80     napi_valuetype paramType;
81     napi_typeof(env, value, &paramType);
82     if (paramType == type) {
83         return true;
84     }
85     return false;
86 }
87 
CreateString(const napi_env env,const std::string & str)88 napi_value NapiUtil::CreateString(const napi_env env, const std::string& str)
89 {
90     napi_value strValue = nullptr;
91     if (napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &strValue) != napi_ok) {
92         HIVIEW_LOGE("failed to create string");
93         return nullptr;
94     }
95     return strValue;
96 }
97 
ThrowError(napi_env env,int code,const std::string & msg,bool isThrow)98 void NapiUtil::ThrowError(napi_env env, int code, const std::string& msg, bool isThrow)
99 {
100     // no error needs to be thrown before api 9
101     if (!isThrow) {
102         return;
103     }
104 
105     if (napi_throw_error(env, std::to_string(code).c_str(), msg.c_str()) != napi_ok) {
106         HIVIEW_LOGE("failed to throw error, code=%{public}d, msg=%{public}s", code, msg.c_str());
107     }
108 }
109 
CreateServiceErrMsg()110 std::string NapiUtil::CreateServiceErrMsg()
111 {
112     return "FaultLogger service is not running or broken.";
113 }
114 
CreateParamCntErrMsg()115 std::string NapiUtil::CreateParamCntErrMsg()
116 {
117     return "The count of input parameters is incorrect.";
118 }
119 
CreateErrMsg(const std::string name)120 std::string NapiUtil::CreateErrMsg(const std::string name)
121 {
122     return "Parameter error. The " + name + " parameter is mandatory.";
123 }
124 
CreateErrMsg(const std::string name,const std::string & type)125 std::string NapiUtil::CreateErrMsg(const std::string name, const std::string& type)
126 {
127     return "Parameter error. The type of " + name + " must be " + type + ".";
128 }
CreateErrMsg(const std::string name,const napi_valuetype type)129 std::string NapiUtil::CreateErrMsg(const std::string name, const napi_valuetype type)
130 {
131     std::string typeStr = "";
132     switch (type) {
133         case napi_number:
134             typeStr = "number";
135             break;
136         case napi_string:
137             typeStr = "string";
138             break;
139         case napi_function:
140             typeStr = "function";
141             break;
142         default:
143             break;
144     }
145     return CreateErrMsg(name, typeStr);
146 }
147 }  // namespace HiviewDFX
148 }  // namespace OHOS
149