• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "underage_model_napi_error.h"
17 
18 #include <optional>
19 #include "fi_log.h"
20 
21 #undef LOG_TAG
22 #define LOG_TAG "DeviceUnderageModelNapiError"
23 
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27 namespace {
28 const std::map<int32_t, std::string> ERROR_MESSAGES = {
29     {PERMISSION_EXCEPTION, "Permission check failed."},
30     {PARAM_EXCEPTION, "Params check failed."},
31     {DEVICE_EXCEPTION, "The device does not support this API."},
32     {SERVICE_EXCEPTION, "Service exception. Possible causes: 1. A system error, such as null pointer, "
33         "container-related exception; 2. N-API invocation exception, invalid N-API status."},
34     {SUBSCRIBE_EXCEPTION, "Subscription failed. Possible causes: 1. Callback registration failure; "
35         "2. Failed to bind native object to js wrapper; 3. N-API invocation exception, invalid N-API status; "
36         "4. IPC request exception."},
37     {UNSUBSCRIBE_EXCEPTION, "Unsubscription failed. Possible causes: 1. Callback failure; "
38         "2. N-API invocation exception, invalid N-API status; 3. IPC request exception."}
39 };
40 } // namespace
41 
CreateUnderageModelNapiError(const napi_env & env,int32_t errorCode,const std::string & errorMsg)42 napi_value CreateUnderageModelNapiError(const napi_env &env, int32_t errorCode, const std::string &errorMsg)
43 {
44     napi_value businessError = nullptr;
45     napi_value code = nullptr;
46     napi_value msg = nullptr;
47     NAPI_CALL(env, napi_create_int32(env, errorCode, &code));
48     NAPI_CALL(env, napi_create_string_utf8(env, errorMsg.c_str(), NAPI_AUTO_LENGTH, &msg));
49     napi_create_error(env, nullptr, msg, &businessError);
50     napi_set_named_property(env, businessError, "code", code);
51     return businessError;
52 }
53 
GetUnderageModelErrMsg(int32_t errorCode)54 std::optional<std::string> GetUnderageModelErrMsg(int32_t errorCode)
55 {
56     auto iter = ERROR_MESSAGES.find(errorCode);
57     if (iter != ERROR_MESSAGES.end()) {
58         return iter->second;
59     }
60     FI_HILOGE("Error, messages not found");
61     return std::nullopt;
62 }
63 
ThrowUnderageModelErr(const napi_env & env,int32_t errorCode,const std::string & printMsg)64 void ThrowUnderageModelErr(const napi_env &env, int32_t errorCode, const std::string &printMsg)
65 {
66     FI_HILOGE("printMsg:%{public}s, errorCode:%{public}d", printMsg.c_str(), errorCode);
67     std::optional<std::string> msg = GetUnderageModelErrMsg(errorCode);
68     if (!msg) {
69         FI_HILOGE("errorCode:%{public}d is invalid", errorCode);
70         return;
71     }
72     napi_value error = CreateUnderageModelNapiError(env, errorCode, msg.value());
73     napi_throw(env, error);
74 }
75 } // namespace DeviceStatus
76 } // namespace Msdp
77 } // namespace OHOS