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