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 #include "on_screen_napi_error.h"
16
17 #include <optional>
18
19 #include "devicestatus_errors.h"
20 #include "fi_log.h"
21 #include "on_screen_data.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "OnScreenNapiError"
25
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 namespace OnScreen {
30 std::map<int32_t, std::string> ERROR_MESSAGES = {
31 { RET_NO_PERMISSION, "Permission check failed." },
32 { RET_NO_SYSTEM_CALLING, "Permission check failed. A non-system application uses the system API." },
33 { RET_PARAM_ERR, "Params check failed." },
34 { RET_NO_SUPPORT, "The device does not support this API." },
35 { RET_SERVICE_EXCEPTION, "Service exception." },
36 { RET_NOT_IN_WHITELIST, "The application or page is not supported." },
37 { RET_WINDOW_ID_ERR, "The window ID is invalid. Possible causes: 1. window id is not passes when"
38 "screen is splited. 2. passed window id is not on screen or floating." },
39 { RET_PAGE_NOT_READY, "The page is not ready." },
40 { RET_TARGET_NOT_FOUND, "The target is not found." },
41 { RET_TIMEOUT, "The request is timeout." },
42 };
43
CreateOnScreenNapiError(const napi_env & env,int32_t errCode,const std::string & errMessage)44 napi_value CreateOnScreenNapiError(const napi_env &env, int32_t errCode, const std::string &errMessage)
45 {
46 napi_value businessError = nullptr;
47 napi_value code = nullptr;
48 napi_value msg = nullptr;
49 NAPI_CALL(env, napi_create_int32(env, errCode, &code));
50 NAPI_CALL(env, napi_create_string_utf8(env, errMessage.c_str(), NAPI_AUTO_LENGTH, &msg));
51 NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError));
52 NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code));
53 return businessError;
54 }
55
GetOnScreenErrMsg(int32_t errCode)56 std::optional<std::string> GetOnScreenErrMsg(int32_t errCode)
57 {
58 auto iter = ERROR_MESSAGES.find(errCode);
59 if (iter != ERROR_MESSAGES.end()) {
60 return iter->second;
61 }
62 FI_HILOGE("Error messages not found");
63 return std::nullopt;
64 }
65
ThrowOnScreenErr(const napi_env & env,int32_t errCode,const std::string & printMsg)66 void ThrowOnScreenErr(const napi_env &env, int32_t errCode, const std::string &printMsg)
67 {
68 errCode = errCode == ETASKS_WAIT_TIMEOUT ? RET_TIMEOUT : errCode;
69 FI_HILOGE("printMsg:%{public}s, errCode:%{public}d", printMsg.c_str(), errCode);
70 std::optional<std::string> msg = GetOnScreenErrMsg(errCode);
71 if (!msg) {
72 FI_HILOGE("errCode:%{public}d is invalid", errCode);
73 return;
74 }
75 napi_value error = CreateOnScreenNapiError(env, errCode, msg.value());
76 napi_throw(env, error);
77 }
78
ThrowOnScreenErrByPromise(const napi_env & env,int32_t errCode,const std::string & printMsg,napi_value & value)79 void ThrowOnScreenErrByPromise(const napi_env &env, int32_t errCode, const std::string &printMsg,
80 napi_value &value)
81 {
82 errCode = errCode == ETASKS_WAIT_TIMEOUT ? RET_TIMEOUT : errCode;
83 FI_HILOGE("printMsg:%{public}s, errCode:%{public}d", printMsg.c_str(), errCode);
84 std::optional<std::string> msg = GetOnScreenErrMsg(errCode);
85 if (!msg) {
86 FI_HILOGE("errCode:%{public}d is invalid", errCode);
87 return;
88 }
89 value = CreateOnScreenNapiError(env, errCode, msg.value());
90 }
91 } // namespace OnScreen
92 } // namespace DeviceStatus
93 } // namespace Msdp
94 } // namespace OHOS