1 /*
2 * Copyright (c) 2023 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
16 #include "audio_haptic_common_napi.h"
17
18 #include "audio_haptic_log.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "AudioHapticCommonNapi"};
22 }
23
24 namespace OHOS {
25 namespace Media {
26 const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter check failed";
27 const std::string NAPI_ERR_OPERATE_NOT_ALLOWED_INFO = "operate not allowed";
28 const std::string NAPI_ERR_IO_ERROR_INFO = "input or output error";
29 const std::string NAPI_ERR_SERVICE_DIED_INFO = "service died";
30 const std::string NAPI_ERR_UNSUPPORTED_FORMAT_INFO = "unsupport format";
31
ThrowError(napi_env env,int32_t code)32 void AudioHapticCommonNapi::ThrowError(napi_env env, int32_t code)
33 {
34 std::string messageValue = AudioHapticCommonNapi::GetMessageByCode(code);
35 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
36 }
37
ThrowError(napi_env env,int32_t code,const std::string & errMessage)38 void AudioHapticCommonNapi::ThrowError(napi_env env, int32_t code, const std::string &errMessage)
39 {
40 napi_throw_error(env, (std::to_string(code)).c_str(), errMessage.c_str());
41 }
42
GetMessageByCode(int32_t & code)43 std::string AudioHapticCommonNapi::GetMessageByCode(int32_t &code)
44 {
45 std::string errMessage;
46 switch (code) {
47 case NAPI_ERR_INPUT_INVALID:
48 errMessage = NAPI_ERR_INPUT_INVALID_INFO;
49 break;
50 case NAPI_ERR_OPERATE_NOT_ALLOWED:
51 errMessage = NAPI_ERR_OPERATE_NOT_ALLOWED_INFO;
52 break;
53 case NAPI_ERR_IO_ERROR:
54 errMessage = NAPI_ERR_IO_ERROR_INFO;
55 break;
56 case NAPI_ERR_SERVICE_DIED:
57 errMessage = NAPI_ERR_SERVICE_DIED_INFO;
58 break;
59 case NAPI_ERR_UNSUPPORTED_FORMAT:
60 errMessage = NAPI_ERR_UNSUPPORTED_FORMAT_INFO;
61 break;
62 default:
63 errMessage = NAPI_ERR_OPERATE_NOT_ALLOWED_INFO;
64 code = NAPI_ERR_OPERATE_NOT_ALLOWED;
65 break;
66 }
67 return errMessage;
68 }
69
GetStringArgument(napi_env env,napi_value value)70 std::string AudioHapticCommonNapi::GetStringArgument(napi_env env, napi_value value)
71 {
72 std::string strValue = "";
73 size_t bufLength = 0;
74 napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
75 if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
76 char *buffer = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
77 CHECK_AND_RETURN_RET_LOG(buffer != nullptr, strValue, "GetStringArgument: no memory");
78 status = napi_get_value_string_utf8(env, value, buffer, bufLength + 1, &bufLength);
79 if (status == napi_ok) {
80 MEDIA_LOGI("GetStringArgument: argument = %{public}s", buffer);
81 strValue = buffer;
82 }
83 free(buffer);
84 buffer = nullptr;
85 }
86 return strValue;
87 }
88 } // namespace Media
89 } // namespace OHOS