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 "ani_err_utils.h"
17 #include "screen_ani_utils.h"
18 #include "window_manager_hilog.h"
19
20 #include <map>
21
22 namespace OHOS::Rosen {
23 constexpr const char* DM_ERROR_MSG_OK = "ok";
24 constexpr const char* DM_ERROR_MSG_INIT_DMS_PROXY_LOCKED = "init dms proxy locked";
25 constexpr const char* DM_ERROR_MSG_IPC_FAILED = "ipc failed";
26 constexpr const char* DM_ERROR_MSG_REMOTE_CREATE_FAILED = "remote create failed";
27 constexpr const char* DM_ERROR_MSG_NULLPTR = "nullptr";
28 constexpr const char* DM_ERROR_MSG_INVALID_PARAM = "invalid param";
29 constexpr const char* DM_ERROR_MSG_WRITE_INTERFACE_TOKEN_FAILED = "write interface token failed";
30 constexpr const char* DM_ERROR_MSG_DEATH_RECIPIENT = "death recipient";
31 constexpr const char* DM_ERROR_MSG_INVALID_MODE_ID = "invalid mode id";
32 constexpr const char* DM_ERROR_MSG_WRITE_DATA_FAILED = "write data failed";
33 constexpr const char* DM_ERROR_MSG_RENDER_SERVICE_FAILED = "render service failed";
34 constexpr const char* DM_ERROR_MSG_UNREGISTER_AGENT_FAILED = "unregister agent failed";
35 constexpr const char* DM_ERROR_MSG_INVALID_CALLING = "invalid calling";
36 constexpr const char* DM_ERROR_MSG_INVALID_PERMISSION = "invalid permission";
37 constexpr const char* DM_ERROR_MSG_NOT_SYSTEM_APP = "not system app";
38 constexpr const char* DM_ERROR_MSG_DEVICE_NOT_SUPPORT = "device not support";
39 constexpr const char* DM_ERROR_MSG_UNKNOWN = "unknown";
40
41 static std::map<DMError, const char*> DM_ERROR_TO_ERROR_MSG_MAP {
42 {DMError::DM_OK, DM_ERROR_MSG_OK },
43 {DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED, DM_ERROR_MSG_INIT_DMS_PROXY_LOCKED },
44 {DMError::DM_ERROR_IPC_FAILED, DM_ERROR_MSG_IPC_FAILED },
45 {DMError::DM_ERROR_REMOTE_CREATE_FAILED, DM_ERROR_MSG_REMOTE_CREATE_FAILED },
46 {DMError::DM_ERROR_NULLPTR, DM_ERROR_MSG_NULLPTR },
47 {DMError::DM_ERROR_INVALID_PARAM, DM_ERROR_MSG_INVALID_PARAM },
48 {DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED, DM_ERROR_MSG_WRITE_INTERFACE_TOKEN_FAILED },
49 {DMError::DM_ERROR_DEATH_RECIPIENT, DM_ERROR_MSG_DEATH_RECIPIENT },
50 {DMError::DM_ERROR_INVALID_MODE_ID, DM_ERROR_MSG_INVALID_MODE_ID },
51 {DMError::DM_ERROR_WRITE_DATA_FAILED, DM_ERROR_MSG_WRITE_DATA_FAILED },
52 {DMError::DM_ERROR_RENDER_SERVICE_FAILED, DM_ERROR_MSG_RENDER_SERVICE_FAILED },
53 {DMError::DM_ERROR_UNREGISTER_AGENT_FAILED, DM_ERROR_MSG_UNREGISTER_AGENT_FAILED },
54 {DMError::DM_ERROR_INVALID_CALLING, DM_ERROR_MSG_INVALID_CALLING },
55 {DMError::DM_ERROR_INVALID_PERMISSION, DM_ERROR_MSG_INVALID_PERMISSION },
56 {DMError::DM_ERROR_NOT_SYSTEM_APP, DM_ERROR_MSG_NOT_SYSTEM_APP },
57 {DMError::DM_ERROR_DEVICE_NOT_SUPPORT, DM_ERROR_MSG_DEVICE_NOT_SUPPORT },
58 {DMError::DM_ERROR_UNKNOWN, DM_ERROR_MSG_UNKNOWN },
59 };
60
61 constexpr const char* DM_ERROR_CODE_MSG_OK = "ok";
62 constexpr const char* DM_ERROR_CODE_MSG_NO_PERMISSION = "no permission";
63 constexpr const char* DM_ERROR_CODE_MSG_NOT_SYSTEM_APP = "not system app";
64 constexpr const char* DM_ERROR_CODE_MSG_INVALID_PARAM = "invalid param";
65 constexpr const char* DM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT = "device not support";
66 constexpr const char* DM_ERROR_CODE_MSG_INVALID_SCREEN = "invalid screen";
67 constexpr const char* DM_ERROR_CODE_MSG_INVALID_CALLING = "invalid calling";
68 constexpr const char* DM_ERROR_CODE_MSG_SYSTEM_INNORMAL = "system innormal";
69
70 static std::map<DmErrorCode, const char*> DM_ERROR_CODE_TO_ERROR_MSG_MAP {
71 {DmErrorCode::DM_OK, DM_ERROR_CODE_MSG_OK },
72 {DmErrorCode::DM_ERROR_NO_PERMISSION, DM_ERROR_CODE_MSG_NO_PERMISSION },
73 {DmErrorCode::DM_ERROR_NOT_SYSTEM_APP, DM_ERROR_CODE_MSG_NOT_SYSTEM_APP },
74 {DmErrorCode::DM_ERROR_INVALID_PARAM, DM_ERROR_CODE_MSG_INVALID_PARAM },
75 {DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT, DM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT },
76 {DmErrorCode::DM_ERROR_INVALID_SCREEN, DM_ERROR_CODE_MSG_INVALID_SCREEN },
77 {DmErrorCode::DM_ERROR_INVALID_CALLING, DM_ERROR_CODE_MSG_INVALID_CALLING },
78 {DmErrorCode::DM_ERROR_SYSTEM_INNORMAL, DM_ERROR_CODE_MSG_SYSTEM_INNORMAL },
79 };
80
GetErrorMsg(const DMError & errorCode)81 std::string AniErrUtils::GetErrorMsg(const DMError& errorCode)
82 {
83 return DM_ERROR_TO_ERROR_MSG_MAP.find(errorCode) != DM_ERROR_TO_ERROR_MSG_MAP.end() ?
84 DM_ERROR_TO_ERROR_MSG_MAP.at(errorCode) : "";
85 }
86
GetErrorMsg(const DmErrorCode & errorCode)87 std::string AniErrUtils::GetErrorMsg(const DmErrorCode& errorCode)
88 {
89 return DM_ERROR_CODE_TO_ERROR_MSG_MAP.find(errorCode) != DM_ERROR_CODE_TO_ERROR_MSG_MAP.end() ?
90 DM_ERROR_CODE_TO_ERROR_MSG_MAP.at(errorCode) : "";
91 }
92
ThrowBusinessError(ani_env * env,DMError error,std::string message)93 ani_status AniErrUtils::ThrowBusinessError(ani_env* env, DMError error, std::string message)
94 {
95 ani_object aniError;
96 CreateBusinessError(env, static_cast<int32_t>(error), message == "" ? GetErrorMsg(error) : message, &aniError);
97 ani_status status = env->ThrowError(static_cast<ani_error>(aniError));
98 if (status != ANI_OK) {
99 TLOGE(WmsLogTag::DMS, "[ANI] fail to throw err, status:%{public}d", static_cast<int32_t>(status));
100 return status;
101 }
102 return ANI_OK;
103 }
104
ThrowBusinessError(ani_env * env,DmErrorCode error,std::string message)105 ani_status AniErrUtils::ThrowBusinessError(ani_env* env, DmErrorCode error, std::string message)
106 {
107 ani_object aniError;
108 CreateBusinessError(env, static_cast<int32_t>(error), message == "" ? GetErrorMsg(error) : message, &aniError);
109 ani_status status = env->ThrowError(static_cast<ani_error>(aniError));
110 if (status != ANI_OK) {
111 TLOGE(WmsLogTag::DMS, "[ANI] fail to throw err, status:%{public}d", static_cast<int32_t>(status));
112 return status;
113 }
114 return ANI_OK;
115 }
116
CreateBusinessError(ani_env * env,int32_t error,std::string message,ani_object * err)117 ani_status AniErrUtils::CreateBusinessError(ani_env* env, int32_t error, std::string message, ani_object* err)
118 {
119 TLOGI(WmsLogTag::DMS, "[ANI] in");
120 ani_class aniClass;
121 ani_status status = env->FindClass("L@ohos/base/BusinessError;", &aniClass);
122 if (status != ANI_OK) {
123 TLOGE(WmsLogTag::DMS, "[ANI] class not found, status:%{public}d", static_cast<int32_t>(status));
124 return status;
125 }
126 ani_method aniCtor;
127 status = env->Class_FindMethod(aniClass, "<ctor>", "Lstd/core/String;Lescompat/ErrorOptions;:V", &aniCtor);
128 if (status != ANI_OK) {
129 TLOGE(WmsLogTag::DMS, "[ANI] ctor not found, status:%{public}d", static_cast<int32_t>(status));
130 return status;
131 }
132 ani_string aniMsg;
133 ScreenAniUtils::GetAniString(env, message, &aniMsg);
134 status = env->Object_New(aniClass, aniCtor, err, aniMsg, ScreenAniUtils::CreateAniUndefined(env));
135 if (status != ANI_OK) {
136 TLOGE(WmsLogTag::DMS, "[ANI] fail to new err, status:%{public}d", static_cast<int32_t>(status));
137 return status;
138 }
139 status = env->Object_SetFieldByName_Double(*err, "code", static_cast<ani_double>(error));
140 if (status != ANI_OK) {
141 TLOGE(WmsLogTag::DMS, "[ANI] fail to set code, status:%{public}d", static_cast<int32_t>(status));
142 return status;
143 }
144 return ANI_OK;
145 }
146 } // namespace OHOS::Rosen