• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_audio_error.h"
17 
18 namespace OHOS {
19 namespace AudioStandard {
ThrowError(napi_env env,const char * napiMessage,int32_t napiCode)20 napi_status NapiAudioError::ThrowError(napi_env env, const char *napiMessage, int32_t napiCode)
21 {
22     napi_value message = nullptr;
23     napi_value code = nullptr;
24     napi_value result = nullptr;
25     napi_create_string_utf8(env, napiMessage, NAPI_AUTO_LENGTH, &message);
26     napi_create_error(env, nullptr, message, &result);
27     napi_create_int32(env, napiCode, &code);
28     napi_set_named_property(env, result, "code", code);
29     napi_throw(env, result);
30     return napi_ok;
31 }
32 
ThrowError(napi_env env,int32_t code)33 void NapiAudioError::ThrowError(napi_env env, int32_t code)
34 {
35     std::string messageValue = GetMessageByCode(code);
36     napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
37 }
38 
ThrowError(napi_env env,int32_t code,const std::string & errMessage)39 void NapiAudioError::ThrowError(napi_env env, int32_t code, const std::string &errMessage)
40 {
41     std::string messageValue;
42     if (code == NAPI_ERR_INVALID_PARAM || code == NAPI_ERR_INPUT_INVALID) {
43         messageValue = errMessage;
44     } else {
45         messageValue = GetMessageByCode(code);
46     }
47     napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
48 }
49 
ThrowErrorAndReturn(napi_env env,int32_t errCode)50 napi_value NapiAudioError::ThrowErrorAndReturn(napi_env env, int32_t errCode)
51 {
52     ThrowError(env, errCode);
53     return nullptr;
54 }
55 
ThrowErrorAndReturn(napi_env env,int32_t errCode,const std::string & errMessage)56 napi_value NapiAudioError::ThrowErrorAndReturn(napi_env env, int32_t errCode, const std::string &errMessage)
57 {
58     ThrowError(env, errCode, errMessage);
59     return nullptr;
60 }
61 
GetMessageByCode(int32_t & code)62 std::string NapiAudioError::GetMessageByCode(int32_t &code)
63 {
64     std::string errMessage;
65     switch (code) {
66         case NAPI_ERR_INVALID_PARAM:
67             errMessage = NAPI_ERR_INVALID_PARAM_INFO;
68             break;
69         case NAPI_ERR_NO_MEMORY:
70             errMessage = NAPI_ERR_NO_MEMORY_INFO;
71             break;
72         case NAPI_ERR_ILLEGAL_STATE:
73             errMessage = NAPI_ERR_ILLEGAL_STATE_INFO;
74             break;
75         case NAPI_ERR_UNSUPPORTED:
76         case ERR_NOT_SUPPORTED:
77             errMessage = NAPI_ERR_UNSUPPORTED_INFO;
78             code = NAPI_ERR_UNSUPPORTED;
79             break;
80         case NAPI_ERR_TIMEOUT:
81             errMessage = NAPI_ERR_TIMEOUT_INFO;
82             break;
83         case NAPI_ERR_STREAM_LIMIT:
84             errMessage = NAPI_ERR_STREAM_LIMIT_INFO;
85             break;
86         case NAPI_ERR_SYSTEM:
87             errMessage = NAPI_ERR_SYSTEM_INFO;
88             break;
89         case NAPI_ERR_INPUT_INVALID:
90             errMessage = NAPI_ERR_INPUT_INVALID_INFO;
91             break;
92         case NAPI_ERR_PERMISSION_DENIED:
93         case ERR_PERMISSION_DENIED:
94             errMessage = NAPI_ERROR_PERMISSION_DENIED_INFO;
95             code = NAPI_ERR_PERMISSION_DENIED;
96             break;
97         case NAPI_ERR_NO_PERMISSION:
98             errMessage = NAPI_ERR_NO_PERMISSION_INFO;
99             break;
100         default:
101             errMessage = NAPI_ERR_SYSTEM_INFO;
102             code = NAPI_ERR_SYSTEM;
103             break;
104     }
105     return errMessage;
106 }
107 } // namespace AudioStandard
108 } // namespace OHOS