1 /*
2 * Copyright (c) 2024 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 "js_err_utils.h"
17
18 #include <map>
19
20 namespace OHOS::Rosen {
21 constexpr const char* WM_ERROR_MSG_OK = "ok";
22 constexpr const char* WM_ERROR_MSG_DO_NOTHING = "do nothing";
23 constexpr const char* WM_ERROR_MSG_NO_MEM = "no mem";
24 constexpr const char* WM_ERROR_MSG_DESTROYED_OBJECT = "destroyed object";
25 constexpr const char* WM_ERROR_MSG_INVALID_WINDOW = "invalid window";
26 constexpr const char* WM_ERROR_MSG_INVALID_WINDOW_MODE_OR_SIZE = "invalid window mode or size";
27 constexpr const char* WM_ERROR_MSG_INVALID_OPERATION = "invalid operation";
28 constexpr const char* WM_ERROR_MSG_INVALID_PERMISSION = "invalid permission";
29 constexpr const char* WM_ERROR_MSG_NOT_SYSTEM_APP = "not system app";
30 constexpr const char* WM_ERROR_MSG_NO_REMOTE_ANIMATION = "no remote animation";
31 constexpr const char* WM_ERROR_MSG_INVALID_DISPLAY = "invalid display";
32 constexpr const char* WM_ERROR_MSG_INVALID_PARENT = "invalid parent";
33 constexpr const char* WM_ERROR_MSG_OPER_FULLSCREEN_FAILED = "oper fullscreen failed";
34 constexpr const char* WM_ERROR_MSG_REPEAT_OPERATION = "repeat operation";
35 constexpr const char* WM_ERROR_MSG_INVALID_SESSION = "invalid session";
36 constexpr const char* WM_ERROR_MSG_INVALID_CALLING = "invalid calling";
37 constexpr const char* WM_ERROR_MSG_DEVICE_NOT_SUPPORT = "device not support";
38 constexpr const char* WM_ERROR_MSG_NEED_REPORT_BASE = "need report base";
39 constexpr const char* WM_ERROR_MSG_NULLPTR = "nullptr";
40 constexpr const char* WM_ERROR_MSG_INVALID_TYPE = "invalid type";
41 constexpr const char* WM_ERROR_MSG_INVALID_PARAM = "invalid param";
42 constexpr const char* WM_ERROR_MSG_SAMGR = "samgr";
43 constexpr const char* WM_ERROR_MSG_IPC_FAILED = "ipc failed";
44 constexpr const char* WM_ERROR_MSG_NEED_REPORT_END = "need report end";
45 constexpr const char* WM_ERROR_MSG_START_ABILITY_FAILED = "start ability failed";
46 constexpr const char* WM_ERROR_MSG_PIP_DESTROY_FAILED = "pip destroy failed";
47 constexpr const char* WM_ERROR_MSG_PIP_STATE_ABNORMALLY = "pip state abnormally";
48 constexpr const char* WM_ERROR_MSG_PIP_CREATE_FAILED = "pip create failed";
49 constexpr const char* WM_ERROR_MSG_PIP_INTERNAL_ERROR = "pip internal error";
50 constexpr const char* WM_ERROR_MSG_PIP_REPEAT_OPERATION = "pip repeat operation";
51
52 static std::map<WMError, const char*> WM_ERROR_TO_ERROR_MSG_MAP {
53 {WMError::WM_OK, WM_ERROR_MSG_OK },
54 {WMError::WM_DO_NOTHING, WM_ERROR_MSG_DO_NOTHING },
55 {WMError::WM_ERROR_NO_MEM, WM_ERROR_MSG_NO_MEM },
56 {WMError::WM_ERROR_DESTROYED_OBJECT, WM_ERROR_MSG_DESTROYED_OBJECT },
57 {WMError::WM_ERROR_INVALID_WINDOW, WM_ERROR_MSG_INVALID_WINDOW },
58 {WMError::WM_ERROR_INVALID_WINDOW_MODE_OR_SIZE, WM_ERROR_MSG_INVALID_WINDOW_MODE_OR_SIZE },
59 {WMError::WM_ERROR_INVALID_OPERATION, WM_ERROR_MSG_INVALID_OPERATION },
60 {WMError::WM_ERROR_INVALID_PERMISSION, WM_ERROR_MSG_INVALID_PERMISSION },
61 {WMError::WM_ERROR_NOT_SYSTEM_APP, WM_ERROR_MSG_NOT_SYSTEM_APP },
62 {WMError::WM_ERROR_NO_REMOTE_ANIMATION, WM_ERROR_MSG_NO_REMOTE_ANIMATION },
63 {WMError::WM_ERROR_INVALID_DISPLAY, WM_ERROR_MSG_INVALID_DISPLAY },
64 {WMError::WM_ERROR_INVALID_PARENT, WM_ERROR_MSG_INVALID_PARENT },
65 {WMError::WM_ERROR_OPER_FULLSCREEN_FAILED, WM_ERROR_MSG_OPER_FULLSCREEN_FAILED },
66 {WMError::WM_ERROR_REPEAT_OPERATION, WM_ERROR_MSG_REPEAT_OPERATION },
67 {WMError::WM_ERROR_INVALID_SESSION, WM_ERROR_MSG_INVALID_SESSION },
68 {WMError::WM_ERROR_INVALID_CALLING, WM_ERROR_MSG_INVALID_CALLING },
69 {WMError::WM_ERROR_DEVICE_NOT_SUPPORT, WM_ERROR_MSG_DEVICE_NOT_SUPPORT },
70 {WMError::WM_ERROR_NEED_REPORT_BASE, WM_ERROR_MSG_NEED_REPORT_BASE },
71 {WMError::WM_ERROR_NULLPTR, WM_ERROR_MSG_NULLPTR },
72 {WMError::WM_ERROR_INVALID_TYPE, WM_ERROR_MSG_INVALID_TYPE },
73 {WMError::WM_ERROR_INVALID_PARAM, WM_ERROR_MSG_INVALID_PARAM },
74 {WMError::WM_ERROR_SAMGR, WM_ERROR_MSG_SAMGR },
75 {WMError::WM_ERROR_IPC_FAILED, WM_ERROR_MSG_IPC_FAILED },
76 {WMError::WM_ERROR_NEED_REPORT_END, WM_ERROR_MSG_NEED_REPORT_END },
77 {WMError::WM_ERROR_START_ABILITY_FAILED, WM_ERROR_MSG_START_ABILITY_FAILED },
78 {WMError::WM_ERROR_PIP_DESTROY_FAILED, WM_ERROR_MSG_PIP_DESTROY_FAILED },
79 {WMError::WM_ERROR_PIP_STATE_ABNORMALLY, WM_ERROR_MSG_PIP_STATE_ABNORMALLY },
80 {WMError::WM_ERROR_PIP_CREATE_FAILED, WM_ERROR_MSG_PIP_CREATE_FAILED },
81 {WMError::WM_ERROR_PIP_INTERNAL_ERROR, WM_ERROR_MSG_PIP_INTERNAL_ERROR },
82 {WMError::WM_ERROR_PIP_REPEAT_OPERATION, WM_ERROR_MSG_PIP_REPEAT_OPERATION },
83 };
84
85 constexpr const char* WM_ERROR_CODE_MSG_OK = "ok";
86 constexpr const char* WM_ERROR_CODE_MSG_NO_PERMISSION = "Permission verification failed. "
87 "The application does not have the permission required to call the API.";
88 constexpr const char* WM_ERROR_CODE_MSG_NOT_SYSTEM_APP = "Permission verification failed. "
89 "A non-system application calls a system API.";
90 constexpr const char* WM_ERROR_CODE_MSG_INVALID_PARAM = "Parameter error. Possible causes: "
91 "1. Mandatory parameters are left unspecified; "
92 "2. Incorrect parameter types; "
93 "3. Parameter verification failed.";
94 constexpr const char* WM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT = "Capability not supported. "
95 "Failed to call the API due to limited device capabilities.";
96 constexpr const char* WM_ERROR_CODE_MSG_REPEAT_OPERATION = "Repeated operation.";
97 constexpr const char* WM_ERROR_CODE_MSG_STATE_ABNORMALLY = "This window state is abnormal.";
98 constexpr const char* WM_ERROR_CODE_MSG_SYSTEM_ABNORMALLY = "This window manager service works abnormally.";
99 constexpr const char* WM_ERROR_CODE_MSG_INVALID_CALLING = "Unauthorized operation.";
100 constexpr const char* WM_ERROR_CODE_MSG_STAGE_ABNORMALLY = "This window stage is abnormal.";
101 constexpr const char* WM_ERROR_CODE_MSG_CONTEXT_ABNORMALLY = "This window context is abnormal.";
102 constexpr const char* WM_ERROR_CODE_MSG_START_ABILITY_FAILED = "Failed to start the ability.";
103 constexpr const char* WM_ERROR_CODE_MSG_INVALID_DISPLAY = "The display device is abnormal.";
104 constexpr const char* WM_ERROR_CODE_MSG_INVALID_PARENT = "The parent window is invalid.";
105 constexpr const char* WM_ERROR_CODE_MSG_OPER_FULLSCREEN_FAILED = "The operation is not supported in full-screen mode.";
106 constexpr const char* WM_ERROR_CODE_MSG_PIP_DESTROY_FAILED = "Failed to destroy the PiP window.";
107 constexpr const char* WM_ERROR_CODE_MSG_PIP_STATE_ABNORMALLY = "The PiP window state is abnormal.";
108 constexpr const char* WM_ERROR_CODE_MSG_PIP_CREATE_FAILED = "Failed to create the PiP window.";
109 constexpr const char* WM_ERROR_CODE_MSG_PIP_INTERNAL_ERROR = "PiP internal error.";
110 constexpr const char* WM_ERROR_CODE_MSG_PIP_REPEAT_OPERATION = "Repeated PiP operation.";
111
112 static std::map<WmErrorCode, const char*> WM_ERROR_CODE_TO_ERROR_MSG_MAP {
113 {WmErrorCode::WM_OK, WM_ERROR_CODE_MSG_OK },
114 {WmErrorCode::WM_ERROR_NO_PERMISSION, WM_ERROR_CODE_MSG_NO_PERMISSION },
115 {WmErrorCode::WM_ERROR_NOT_SYSTEM_APP, WM_ERROR_CODE_MSG_NOT_SYSTEM_APP },
116 {WmErrorCode::WM_ERROR_INVALID_PARAM, WM_ERROR_CODE_MSG_INVALID_PARAM },
117 {WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT, WM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT },
118 {WmErrorCode::WM_ERROR_REPEAT_OPERATION, WM_ERROR_CODE_MSG_REPEAT_OPERATION },
119 {WmErrorCode::WM_ERROR_STATE_ABNORMALLY, WM_ERROR_CODE_MSG_STATE_ABNORMALLY },
120 {WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY, WM_ERROR_CODE_MSG_SYSTEM_ABNORMALLY },
121 {WmErrorCode::WM_ERROR_INVALID_CALLING, WM_ERROR_CODE_MSG_INVALID_CALLING },
122 {WmErrorCode::WM_ERROR_STAGE_ABNORMALLY, WM_ERROR_CODE_MSG_STAGE_ABNORMALLY },
123 {WmErrorCode::WM_ERROR_CONTEXT_ABNORMALLY, WM_ERROR_CODE_MSG_CONTEXT_ABNORMALLY },
124 {WmErrorCode::WM_ERROR_START_ABILITY_FAILED, WM_ERROR_CODE_MSG_START_ABILITY_FAILED },
125 {WmErrorCode::WM_ERROR_INVALID_DISPLAY, WM_ERROR_CODE_MSG_INVALID_DISPLAY },
126 {WmErrorCode::WM_ERROR_INVALID_PARENT, WM_ERROR_CODE_MSG_INVALID_PARENT },
127 {WmErrorCode::WM_ERROR_OPER_FULLSCREEN_FAILED, WM_ERROR_CODE_MSG_OPER_FULLSCREEN_FAILED },
128 {WmErrorCode::WM_ERROR_PIP_DESTROY_FAILED, WM_ERROR_CODE_MSG_PIP_DESTROY_FAILED },
129 {WmErrorCode::WM_ERROR_PIP_STATE_ABNORMALLY, WM_ERROR_CODE_MSG_PIP_STATE_ABNORMALLY },
130 {WmErrorCode::WM_ERROR_PIP_CREATE_FAILED, WM_ERROR_CODE_MSG_PIP_CREATE_FAILED },
131 {WmErrorCode::WM_ERROR_PIP_INTERNAL_ERROR, WM_ERROR_CODE_MSG_PIP_INTERNAL_ERROR },
132 {WmErrorCode::WM_ERROR_PIP_REPEAT_OPERATION, WM_ERROR_CODE_MSG_PIP_REPEAT_OPERATION },
133 };
134
135 constexpr const char* DM_ERROR_MSG_OK = "ok";
136 constexpr const char* DM_ERROR_MSG_INIT_DMS_PROXY_LOCKED = "init dms proxy locked";
137 constexpr const char* DM_ERROR_MSG_IPC_FAILED = "ipc failed";
138 constexpr const char* DM_ERROR_MSG_REMOTE_CREATE_FAILED = "remote create failed";
139 constexpr const char* DM_ERROR_MSG_NULLPTR = "nullptr";
140 constexpr const char* DM_ERROR_MSG_INVALID_PARAM = "invalid param";
141 constexpr const char* DM_ERROR_MSG_WRITE_INTERFACE_TOKEN_FAILED = "write interface token failed";
142 constexpr const char* DM_ERROR_MSG_DEATH_RECIPIENT = "death recipient";
143 constexpr const char* DM_ERROR_MSG_INVALID_MODE_ID = "invalid mode id";
144 constexpr const char* DM_ERROR_MSG_WRITE_DATA_FAILED = "write data failed";
145 constexpr const char* DM_ERROR_MSG_RENDER_SERVICE_FAILED = "render service failed";
146 constexpr const char* DM_ERROR_MSG_UNREGISTER_AGENT_FAILED = "unregister agent failed";
147 constexpr const char* DM_ERROR_MSG_INVALID_CALLING = "invalid calling";
148 constexpr const char* DM_ERROR_MSG_INVALID_PERMISSION = "invalid permission";
149 constexpr const char* DM_ERROR_MSG_NOT_SYSTEM_APP = "not system app";
150 constexpr const char* DM_ERROR_MSG_DEVICE_NOT_SUPPORT = "device not support";
151 constexpr const char* DM_ERROR_MSG_UNKNOWN = "unknown";
152
153 static std::map<DMError, const char*> DM_ERROR_TO_ERROR_MSG_MAP {
154 {DMError::DM_OK, DM_ERROR_MSG_OK },
155 {DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED, DM_ERROR_MSG_INIT_DMS_PROXY_LOCKED },
156 {DMError::DM_ERROR_IPC_FAILED, DM_ERROR_MSG_IPC_FAILED },
157 {DMError::DM_ERROR_REMOTE_CREATE_FAILED, DM_ERROR_MSG_REMOTE_CREATE_FAILED },
158 {DMError::DM_ERROR_NULLPTR, DM_ERROR_MSG_NULLPTR },
159 {DMError::DM_ERROR_INVALID_PARAM, DM_ERROR_MSG_INVALID_PARAM },
160 {DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED, DM_ERROR_MSG_WRITE_INTERFACE_TOKEN_FAILED },
161 {DMError::DM_ERROR_DEATH_RECIPIENT, DM_ERROR_MSG_DEATH_RECIPIENT },
162 {DMError::DM_ERROR_INVALID_MODE_ID, DM_ERROR_MSG_INVALID_MODE_ID },
163 {DMError::DM_ERROR_WRITE_DATA_FAILED, DM_ERROR_MSG_WRITE_DATA_FAILED },
164 {DMError::DM_ERROR_RENDER_SERVICE_FAILED, DM_ERROR_MSG_RENDER_SERVICE_FAILED },
165 {DMError::DM_ERROR_UNREGISTER_AGENT_FAILED, DM_ERROR_MSG_UNREGISTER_AGENT_FAILED },
166 {DMError::DM_ERROR_INVALID_CALLING, DM_ERROR_MSG_INVALID_CALLING },
167 {DMError::DM_ERROR_INVALID_PERMISSION, DM_ERROR_MSG_INVALID_PERMISSION },
168 {DMError::DM_ERROR_NOT_SYSTEM_APP, DM_ERROR_MSG_NOT_SYSTEM_APP },
169 {DMError::DM_ERROR_DEVICE_NOT_SUPPORT, DM_ERROR_MSG_DEVICE_NOT_SUPPORT },
170 {DMError::DM_ERROR_UNKNOWN, DM_ERROR_MSG_UNKNOWN },
171 };
172
173 constexpr const char* DM_ERROR_CODE_MSG_OK = "ok";
174 constexpr const char* DM_ERROR_CODE_MSG_NO_PERMISSION = "no permission";
175 constexpr const char* DM_ERROR_CODE_MSG_NOT_SYSTEM_APP = "not system app";
176 constexpr const char* DM_ERROR_CODE_MSG_INVALID_PARAM = "invalid param";
177 constexpr const char* DM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT = "device not support";
178 constexpr const char* DM_ERROR_CODE_MSG_INVALID_SCREEN = "invalid screen";
179 constexpr const char* DM_ERROR_CODE_MSG_INVALID_CALLING = "invalid calling";
180 constexpr const char* DM_ERROR_CODE_MSG_SYSTEM_INNORMAL = "system innormal";
181
182 static std::map<DmErrorCode, const char*> DM_ERROR_CODE_TO_ERROR_MSG_MAP {
183 {DmErrorCode::DM_OK, DM_ERROR_CODE_MSG_OK },
184 {DmErrorCode::DM_ERROR_NO_PERMISSION, DM_ERROR_CODE_MSG_NO_PERMISSION },
185 {DmErrorCode::DM_ERROR_NOT_SYSTEM_APP, DM_ERROR_CODE_MSG_NOT_SYSTEM_APP },
186 {DmErrorCode::DM_ERROR_INVALID_PARAM, DM_ERROR_CODE_MSG_INVALID_PARAM },
187 {DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT, DM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT },
188 {DmErrorCode::DM_ERROR_INVALID_SCREEN, DM_ERROR_CODE_MSG_INVALID_SCREEN },
189 {DmErrorCode::DM_ERROR_INVALID_CALLING, DM_ERROR_CODE_MSG_INVALID_CALLING },
190 {DmErrorCode::DM_ERROR_SYSTEM_INNORMAL, DM_ERROR_CODE_MSG_SYSTEM_INNORMAL },
191 };
192
193 template<class T>
CreateJsValue(napi_env env, const T& value)194 napi_value JsErrUtils::CreateJsValue(napi_env env, const T& value)
195 {
196 using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
197 napi_value result = nullptr;
198 if constexpr (std::is_same_v<ValueType, bool>) {
199 napi_get_boolean(env, value, &result);
200 return result;
201 } else if constexpr (std::is_arithmetic_v<ValueType>) {
202 return CreateJsNumber(env, value);
203 } else if constexpr (std::is_same_v<ValueType, std::string>) {
204 napi_create_string_utf8(env, value.c_str(), value.length(), &result);
205 return result;
206 } else if constexpr (std::is_enum_v<ValueType>) {
207 return CreateJsNumber(env, static_cast<std::make_signed_t<ValueType>>(value));
208 } else if constexpr (std::is_same_v<ValueType, const char*>) {
209 (value != nullptr) ? napi_create_string_utf8(env, value, strlen(value), &result) :
210 napi_get_undefined(env, &result);
211 return result;
212 }
213 }
214
GetErrorMsg(const WMError& errorCode)215 std::string JsErrUtils::GetErrorMsg(const WMError& errorCode)
216 {
217 return WM_ERROR_TO_ERROR_MSG_MAP.find(errorCode) != WM_ERROR_TO_ERROR_MSG_MAP.end() ?
218 WM_ERROR_TO_ERROR_MSG_MAP.at(errorCode) : "";
219 }
220
GetErrorMsg(const WmErrorCode& errorCode)221 std::string JsErrUtils::GetErrorMsg(const WmErrorCode& errorCode)
222 {
223 return WM_ERROR_CODE_TO_ERROR_MSG_MAP.find(errorCode) != WM_ERROR_CODE_TO_ERROR_MSG_MAP.end() ?
224 WM_ERROR_CODE_TO_ERROR_MSG_MAP.at(errorCode) : "";
225 }
226
GetErrorMsg(const DMError& errorCode)227 std::string JsErrUtils::GetErrorMsg(const DMError& errorCode)
228 {
229 return DM_ERROR_TO_ERROR_MSG_MAP.find(errorCode) != DM_ERROR_TO_ERROR_MSG_MAP.end() ?
230 DM_ERROR_TO_ERROR_MSG_MAP.at(errorCode) : "";
231 }
232
GetErrorMsg(const DmErrorCode& errorCode)233 std::string JsErrUtils::GetErrorMsg(const DmErrorCode& errorCode)
234 {
235 return DM_ERROR_CODE_TO_ERROR_MSG_MAP.find(errorCode) != DM_ERROR_CODE_TO_ERROR_MSG_MAP.end() ?
236 DM_ERROR_CODE_TO_ERROR_MSG_MAP.at(errorCode) : "";
237 }
238
CreateJsError(napi_env env, const WMError& errorCode, std::string msg)239 napi_value JsErrUtils::CreateJsError(napi_env env, const WMError& errorCode, std::string msg)
240 {
241 napi_value result = nullptr;
242 napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
243 CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
244 return result;
245 }
246
CreateJsError(napi_env env, const WmErrorCode& errorCode, std::string msg)247 napi_value JsErrUtils::CreateJsError(napi_env env, const WmErrorCode& errorCode, std::string msg)
248 {
249 napi_value result = nullptr;
250 napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
251 CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
252 return result;
253 }
254
CreateJsError(napi_env env, const DMError& errorCode, std::string msg)255 napi_value JsErrUtils::CreateJsError(napi_env env, const DMError& errorCode, std::string msg)
256 {
257 napi_value result = nullptr;
258 napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
259 CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
260 return result;
261 }
262
CreateJsError(napi_env env, const DmErrorCode& errorCode, std::string msg)263 napi_value JsErrUtils::CreateJsError(napi_env env, const DmErrorCode& errorCode, std::string msg)
264 {
265 napi_value result = nullptr;
266 napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
267 CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
268 return result;
269 }
270 } // namespace OHOS::Rosen
271