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