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_INVALID_OP_IN_CUR_STATUS = "invalid operation in the current window status";
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_INVALID_OP_IN_CUR_STATUS,        WM_ERROR_MSG_INVALID_OP_IN_CUR_STATUS    },
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_INVALID_OP_IN_CUR_STATUS =
106     "The operation in the current window status is invalid.";
107 constexpr const char* WM_ERROR_CODE_MSG_PIP_DESTROY_FAILED = "Failed to destroy the PiP window.";
108 constexpr const char* WM_ERROR_CODE_MSG_PIP_STATE_ABNORMALLY = "The PiP window state is abnormal.";
109 constexpr const char* WM_ERROR_CODE_MSG_PIP_CREATE_FAILED = "Failed to create the PiP window.";
110 constexpr const char* WM_ERROR_CODE_MSG_PIP_INTERNAL_ERROR = "PiP internal error.";
111 constexpr const char* WM_ERROR_CODE_MSG_PIP_REPEAT_OPERATION = "Repeated PiP operation.";
112 
113 static std::map<WmErrorCode, const char*> WM_ERROR_CODE_TO_ERROR_MSG_MAP {
114     {WmErrorCode::WM_OK,                              WM_ERROR_CODE_MSG_OK                       },
115     {WmErrorCode::WM_ERROR_NO_PERMISSION,             WM_ERROR_CODE_MSG_NO_PERMISSION            },
116     {WmErrorCode::WM_ERROR_NOT_SYSTEM_APP,            WM_ERROR_CODE_MSG_NOT_SYSTEM_APP           },
117     {WmErrorCode::WM_ERROR_INVALID_PARAM,             WM_ERROR_CODE_MSG_INVALID_PARAM            },
118     {WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT,        WM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT       },
119     {WmErrorCode::WM_ERROR_REPEAT_OPERATION,          WM_ERROR_CODE_MSG_REPEAT_OPERATION         },
120     {WmErrorCode::WM_ERROR_STATE_ABNORMALLY,          WM_ERROR_CODE_MSG_STATE_ABNORMALLY         },
121     {WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY,         WM_ERROR_CODE_MSG_SYSTEM_ABNORMALLY        },
122     {WmErrorCode::WM_ERROR_INVALID_CALLING,           WM_ERROR_CODE_MSG_INVALID_CALLING          },
123     {WmErrorCode::WM_ERROR_STAGE_ABNORMALLY,          WM_ERROR_CODE_MSG_STAGE_ABNORMALLY         },
124     {WmErrorCode::WM_ERROR_CONTEXT_ABNORMALLY,        WM_ERROR_CODE_MSG_CONTEXT_ABNORMALLY       },
125     {WmErrorCode::WM_ERROR_START_ABILITY_FAILED,      WM_ERROR_CODE_MSG_START_ABILITY_FAILED     },
126     {WmErrorCode::WM_ERROR_INVALID_DISPLAY,           WM_ERROR_CODE_MSG_INVALID_DISPLAY          },
127     {WmErrorCode::WM_ERROR_INVALID_PARENT,            WM_ERROR_CODE_MSG_INVALID_PARENT           },
128     {WmErrorCode::WM_ERROR_INVALID_OP_IN_CUR_STATUS,  WM_ERROR_CODE_MSG_INVALID_OP_IN_CUR_STATUS },
129     {WmErrorCode::WM_ERROR_PIP_DESTROY_FAILED,        WM_ERROR_CODE_MSG_PIP_DESTROY_FAILED       },
130     {WmErrorCode::WM_ERROR_PIP_STATE_ABNORMALLY,      WM_ERROR_CODE_MSG_PIP_STATE_ABNORMALLY     },
131     {WmErrorCode::WM_ERROR_PIP_CREATE_FAILED,         WM_ERROR_CODE_MSG_PIP_CREATE_FAILED        },
132     {WmErrorCode::WM_ERROR_PIP_INTERNAL_ERROR,        WM_ERROR_CODE_MSG_PIP_INTERNAL_ERROR       },
133     {WmErrorCode::WM_ERROR_PIP_REPEAT_OPERATION,      WM_ERROR_CODE_MSG_PIP_REPEAT_OPERATION     },
134 };
135 
136 constexpr const char* DM_ERROR_MSG_OK = "ok";
137 constexpr const char* DM_ERROR_MSG_INIT_DMS_PROXY_LOCKED = "init dms proxy locked";
138 constexpr const char* DM_ERROR_MSG_IPC_FAILED = "ipc failed";
139 constexpr const char* DM_ERROR_MSG_REMOTE_CREATE_FAILED = "remote create failed";
140 constexpr const char* DM_ERROR_MSG_NULLPTR = "nullptr";
141 constexpr const char* DM_ERROR_MSG_INVALID_PARAM = "invalid param";
142 constexpr const char* DM_ERROR_MSG_WRITE_INTERFACE_TOKEN_FAILED = "write interface token failed";
143 constexpr const char* DM_ERROR_MSG_DEATH_RECIPIENT = "death recipient";
144 constexpr const char* DM_ERROR_MSG_INVALID_MODE_ID = "invalid mode id";
145 constexpr const char* DM_ERROR_MSG_WRITE_DATA_FAILED = "write data failed";
146 constexpr const char* DM_ERROR_MSG_RENDER_SERVICE_FAILED = "render service failed";
147 constexpr const char* DM_ERROR_MSG_UNREGISTER_AGENT_FAILED = "unregister agent failed";
148 constexpr const char* DM_ERROR_MSG_INVALID_CALLING = "invalid calling";
149 constexpr const char* DM_ERROR_MSG_INVALID_PERMISSION = "invalid permission";
150 constexpr const char* DM_ERROR_MSG_NOT_SYSTEM_APP = "not system app";
151 constexpr const char* DM_ERROR_MSG_DEVICE_NOT_SUPPORT = "device not support";
152 constexpr const char* DM_ERROR_MSG_UNKNOWN = "unknown";
153 
154 static std::map<DMError, const char*> DM_ERROR_TO_ERROR_MSG_MAP {
155     {DMError::DM_OK,                                    DM_ERROR_MSG_OK                           },
156     {DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED,           DM_ERROR_MSG_INIT_DMS_PROXY_LOCKED        },
157     {DMError::DM_ERROR_IPC_FAILED,                      DM_ERROR_MSG_IPC_FAILED                   },
158     {DMError::DM_ERROR_REMOTE_CREATE_FAILED,            DM_ERROR_MSG_REMOTE_CREATE_FAILED         },
159     {DMError::DM_ERROR_NULLPTR,                         DM_ERROR_MSG_NULLPTR                      },
160     {DMError::DM_ERROR_INVALID_PARAM,                   DM_ERROR_MSG_INVALID_PARAM                },
161     {DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED,    DM_ERROR_MSG_WRITE_INTERFACE_TOKEN_FAILED },
162     {DMError::DM_ERROR_DEATH_RECIPIENT,                 DM_ERROR_MSG_DEATH_RECIPIENT              },
163     {DMError::DM_ERROR_INVALID_MODE_ID,                 DM_ERROR_MSG_INVALID_MODE_ID              },
164     {DMError::DM_ERROR_WRITE_DATA_FAILED,               DM_ERROR_MSG_WRITE_DATA_FAILED            },
165     {DMError::DM_ERROR_RENDER_SERVICE_FAILED,           DM_ERROR_MSG_RENDER_SERVICE_FAILED        },
166     {DMError::DM_ERROR_UNREGISTER_AGENT_FAILED,         DM_ERROR_MSG_UNREGISTER_AGENT_FAILED      },
167     {DMError::DM_ERROR_INVALID_CALLING,                 DM_ERROR_MSG_INVALID_CALLING              },
168     {DMError::DM_ERROR_INVALID_PERMISSION,              DM_ERROR_MSG_INVALID_PERMISSION           },
169     {DMError::DM_ERROR_NOT_SYSTEM_APP,                  DM_ERROR_MSG_NOT_SYSTEM_APP               },
170     {DMError::DM_ERROR_DEVICE_NOT_SUPPORT,              DM_ERROR_MSG_DEVICE_NOT_SUPPORT           },
171     {DMError::DM_ERROR_UNKNOWN,                         DM_ERROR_MSG_UNKNOWN                      },
172 };
173 
174 constexpr const char* DM_ERROR_CODE_MSG_OK = "ok";
175 constexpr const char* DM_ERROR_CODE_MSG_NO_PERMISSION = "no permission";
176 constexpr const char* DM_ERROR_CODE_MSG_NOT_SYSTEM_APP = "not system app";
177 constexpr const char* DM_ERROR_CODE_MSG_INVALID_PARAM = "invalid param";
178 constexpr const char* DM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT = "device not support";
179 constexpr const char* DM_ERROR_CODE_MSG_INVALID_SCREEN = "invalid screen";
180 constexpr const char* DM_ERROR_CODE_MSG_INVALID_CALLING = "invalid calling";
181 constexpr const char* DM_ERROR_CODE_MSG_SYSTEM_INNORMAL = "system innormal";
182 
183 static std::map<DmErrorCode, const char*> DM_ERROR_CODE_TO_ERROR_MSG_MAP {
184     {DmErrorCode::DM_OK,                              DM_ERROR_CODE_MSG_OK                 },
185     {DmErrorCode::DM_ERROR_NO_PERMISSION,             DM_ERROR_CODE_MSG_NO_PERMISSION      },
186     {DmErrorCode::DM_ERROR_NOT_SYSTEM_APP,            DM_ERROR_CODE_MSG_NOT_SYSTEM_APP     },
187     {DmErrorCode::DM_ERROR_INVALID_PARAM,             DM_ERROR_CODE_MSG_INVALID_PARAM      },
188     {DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT,        DM_ERROR_CODE_MSG_DEVICE_NOT_SUPPORT },
189     {DmErrorCode::DM_ERROR_INVALID_SCREEN,            DM_ERROR_CODE_MSG_INVALID_SCREEN     },
190     {DmErrorCode::DM_ERROR_INVALID_CALLING,           DM_ERROR_CODE_MSG_INVALID_CALLING    },
191     {DmErrorCode::DM_ERROR_SYSTEM_INNORMAL,           DM_ERROR_CODE_MSG_SYSTEM_INNORMAL    },
192 };
193 
194 template<class T>
CreateJsValue(napi_env env,const T & value)195 napi_value JsErrUtils::CreateJsValue(napi_env env, const T& value)
196 {
197     using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
198     napi_value result = nullptr;
199     if constexpr (std::is_same_v<ValueType, bool>) {
200         napi_get_boolean(env, value, &result);
201         return result;
202     } else if constexpr (std::is_arithmetic_v<ValueType>) {
203         return CreateJsNumber(env, value);
204     } else if constexpr (std::is_same_v<ValueType, std::string>) {
205         napi_create_string_utf8(env, value.c_str(), value.length(), &result);
206         return result;
207     } else if constexpr (std::is_enum_v<ValueType>) {
208         return CreateJsNumber(env, static_cast<std::make_signed_t<ValueType>>(value));
209     } else if constexpr (std::is_same_v<ValueType, const char*>) {
210         (value != nullptr) ? napi_create_string_utf8(env, value, strlen(value), &result) :
211             napi_get_undefined(env, &result);
212         return result;
213     }
214 }
215 
GetErrorMsg(const WMError & errorCode)216 std::string JsErrUtils::GetErrorMsg(const WMError& errorCode)
217 {
218     return WM_ERROR_TO_ERROR_MSG_MAP.find(errorCode) != WM_ERROR_TO_ERROR_MSG_MAP.end() ?
219         WM_ERROR_TO_ERROR_MSG_MAP.at(errorCode) : "";
220 }
221 
GetErrorMsg(const WmErrorCode & errorCode)222 std::string JsErrUtils::GetErrorMsg(const WmErrorCode& errorCode)
223 {
224     return WM_ERROR_CODE_TO_ERROR_MSG_MAP.find(errorCode) != WM_ERROR_CODE_TO_ERROR_MSG_MAP.end() ?
225         WM_ERROR_CODE_TO_ERROR_MSG_MAP.at(errorCode) : "";
226 }
227 
GetErrorMsg(const DMError & errorCode)228 std::string JsErrUtils::GetErrorMsg(const DMError& errorCode)
229 {
230     return DM_ERROR_TO_ERROR_MSG_MAP.find(errorCode) != DM_ERROR_TO_ERROR_MSG_MAP.end() ?
231         DM_ERROR_TO_ERROR_MSG_MAP.at(errorCode) : "";
232 }
233 
GetErrorMsg(const DmErrorCode & errorCode)234 std::string JsErrUtils::GetErrorMsg(const DmErrorCode& errorCode)
235 {
236     return DM_ERROR_CODE_TO_ERROR_MSG_MAP.find(errorCode) != DM_ERROR_CODE_TO_ERROR_MSG_MAP.end() ?
237         DM_ERROR_CODE_TO_ERROR_MSG_MAP.at(errorCode) : "";
238 }
239 
CreateJsError(napi_env env,const WMError & errorCode,std::string msg)240 napi_value JsErrUtils::CreateJsError(napi_env env, const WMError& errorCode, std::string msg)
241 {
242     napi_value result = nullptr;
243     napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
244         CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
245     return result;
246 }
247 
CreateJsError(napi_env env,const WmErrorCode & errorCode,std::string msg)248 napi_value JsErrUtils::CreateJsError(napi_env env, const WmErrorCode& errorCode, std::string msg)
249 {
250     napi_value result = nullptr;
251     napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
252         CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
253     return result;
254 }
255 
CreateJsError(napi_env env,const DMError & errorCode,std::string msg)256 napi_value JsErrUtils::CreateJsError(napi_env env, const DMError& errorCode, std::string msg)
257 {
258     napi_value result = nullptr;
259     napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
260         CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
261     return result;
262 }
263 
CreateJsError(napi_env env,const DmErrorCode & errorCode,std::string msg)264 napi_value JsErrUtils::CreateJsError(napi_env env, const DmErrorCode& errorCode, std::string msg)
265 {
266     napi_value result = nullptr;
267     napi_create_error(env, CreateJsValue(env, static_cast<int32_t>(errorCode)),
268         CreateJsValue(env, msg == "" ? GetErrorMsg(errorCode) : msg), &result);
269     return result;
270 }
271 } // namespace OHOS::Rosen
272