1 /*
2 * Copyright (c) 2023-2025 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 "napi_audio_error.h"
17
18 namespace OHOS {
19 namespace AudioStandard {
20 const std::string NAPI_ERROR_INVALID_PARAM_INFO = "input parameter value error";
21 const std::string NAPI_ERROR_PERMISSION_DENIED_INFO = "not system app";
22 const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter type or number mismatch";
23 const std::string NAPI_ERR_INVALID_PARAM_INFO = "invalid parameter";
24 const std::string NAPI_ERR_NO_MEMORY_INFO = "allocate memory failed";
25 const std::string NAPI_ERR_ILLEGAL_STATE_INFO = "Operation not permit at current state";
26 const std::string NAPI_ERR_UNSUPPORTED_INFO = "unsupported option";
27 const std::string NAPI_ERR_TIMEOUT_INFO = "time out";
28 const std::string NAPI_ERR_STREAM_LIMIT_INFO = "stream number limited";
29 const std::string NAPI_ERR_SYSTEM_INFO = "system error";
30 const std::string NAPI_ERR_NO_PERMISSION_INFO = "permission denied";
31
ThrowError(napi_env env,const char * napiMessage,int32_t napiCode)32 napi_status NapiAudioError::ThrowError(napi_env env, const char *napiMessage, int32_t napiCode)
33 {
34 napi_value message = nullptr;
35 napi_value code = nullptr;
36 napi_value result = nullptr;
37 napi_create_string_utf8(env, napiMessage, NAPI_AUTO_LENGTH, &message);
38 napi_create_error(env, nullptr, message, &result);
39 napi_create_int32(env, napiCode, &code);
40 napi_set_named_property(env, result, "code", code);
41 napi_throw(env, result);
42 return napi_ok;
43 }
44
ThrowError(napi_env env,int32_t code)45 void NapiAudioError::ThrowError(napi_env env, int32_t code)
46 {
47 std::string messageValue = GetMessageByCode(code);
48 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
49 }
50
ThrowError(napi_env env,int32_t code,const std::string & errMessage)51 void NapiAudioError::ThrowError(napi_env env, int32_t code, const std::string &errMessage)
52 {
53 std::string messageValue;
54 if (code == NAPI_ERR_INVALID_PARAM || code == NAPI_ERR_INPUT_INVALID) {
55 messageValue = errMessage;
56 } else {
57 messageValue = GetMessageByCode(code);
58 }
59 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
60 }
61
ThrowErrorAndReturn(napi_env env,int32_t errCode)62 napi_value NapiAudioError::ThrowErrorAndReturn(napi_env env, int32_t errCode)
63 {
64 ThrowError(env, errCode);
65 return nullptr;
66 }
67
ThrowErrorAndReturn(napi_env env,int32_t errCode,const std::string & errMessage)68 napi_value NapiAudioError::ThrowErrorAndReturn(napi_env env, int32_t errCode, const std::string &errMessage)
69 {
70 ThrowError(env, errCode, errMessage);
71 return nullptr;
72 }
73
GetMessageByCode(int32_t & code)74 std::string NapiAudioError::GetMessageByCode(int32_t &code)
75 {
76 std::string errMessage;
77 switch (code) {
78 case NAPI_ERR_INVALID_PARAM:
79 case ERR_INVALID_PARAM:
80 errMessage = NAPI_ERR_INVALID_PARAM_INFO;
81 code = NAPI_ERR_INVALID_PARAM;
82 break;
83 case NAPI_ERR_NO_MEMORY:
84 errMessage = NAPI_ERR_NO_MEMORY_INFO;
85 break;
86 case NAPI_ERR_ILLEGAL_STATE:
87 errMessage = NAPI_ERR_ILLEGAL_STATE_INFO;
88 break;
89 case NAPI_ERR_UNSUPPORTED:
90 case ERR_NOT_SUPPORTED:
91 errMessage = NAPI_ERR_UNSUPPORTED_INFO;
92 code = NAPI_ERR_UNSUPPORTED;
93 break;
94 case NAPI_ERR_TIMEOUT:
95 errMessage = NAPI_ERR_TIMEOUT_INFO;
96 break;
97 case NAPI_ERR_STREAM_LIMIT:
98 errMessage = NAPI_ERR_STREAM_LIMIT_INFO;
99 break;
100 case NAPI_ERR_SYSTEM:
101 errMessage = NAPI_ERR_SYSTEM_INFO;
102 break;
103 case NAPI_ERR_INPUT_INVALID:
104 errMessage = NAPI_ERR_INPUT_INVALID_INFO;
105 break;
106 case NAPI_ERR_PERMISSION_DENIED:
107 case ERR_SYSTEM_PERMISSION_DENIED:
108 errMessage = NAPI_ERROR_PERMISSION_DENIED_INFO;
109 code = NAPI_ERR_PERMISSION_DENIED;
110 break;
111 case NAPI_ERR_NO_PERMISSION:
112 case ERR_PERMISSION_DENIED:
113 errMessage = NAPI_ERR_NO_PERMISSION_INFO;
114 code = NAPI_ERR_NO_PERMISSION;
115 break;
116 default:
117 errMessage = NAPI_ERR_SYSTEM_INFO;
118 code = NAPI_ERR_SYSTEM;
119 break;
120 }
121 return errMessage;
122 }
123 } // namespace AudioStandard
124 } // namespace OHOS