• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "napi_edm_error.h"
17 
18 #include <unordered_map>
19 
20 namespace OHOS {
21 namespace EDM {
22 static const std::unordered_map<int32_t, std::string> errMessageMap = {
23     {EdmReturnErrCode::PERMISSION_DENIED,
24         "Permission verification failed. The application does not have the permission required to call the API."},
25     {EdmReturnErrCode::SYSTEM_ABNORMALLY, "The system ability works abnormally."},
26     {EdmReturnErrCode::ENABLE_ADMIN_FAILED, "Failed to activate the administrator application of the device."},
27     {EdmReturnErrCode::COMPONENT_INVALID, "The administrator ability component is invalid."},
28     {EdmReturnErrCode::ADMIN_INACTIVE, "The application is not an administrator application of the device."},
29     {EdmReturnErrCode::DISABLE_ADMIN_FAILED, "Failed to deactivate the administrator application of the device."},
30     {EdmReturnErrCode::UID_INVALID, "The specified user ID is invalid."},
31     {EdmReturnErrCode::INTERFACE_UNSUPPORTED,
32         "Capability not supported. Failed to call the API due to limited device capabilities."},
33     {EdmReturnErrCode::PARAM_ERROR, "Parameter error."},
34     {EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED,
35         "The administrator application does not have permission to manage the device."},
36     {EdmReturnErrCode::MANAGED_EVENTS_INVALID, "The specified system event is invalid."},
37     {EdmReturnErrCode::SYSTEM_API_DENIED,
38         "Permission verification failed. A non-system application calls a system API."},
39     {EdmReturnErrCode::MANAGED_CERTIFICATE_FAILED, "Failed to manage the certificate. $."},
40     {EdmReturnErrCode::AUTHORIZE_PERMISSION_FAILED, "Failed to grant the permission to the application."},
41     {EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED, "A conflict policy has been configured."},
42     {EdmReturnErrCode::APPLICATION_INSTALL_FAILED, "Failed to install the application. $."},
43     {EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED, "Failed to add an OS account."},
44     {EdmReturnErrCode::UPGRADE_PACKAGES_ANALYZE_FAILED, "the upgrade packages do not exist or analyzing failed, $."},
45     {EdmReturnErrCode::ADD_KEEP_ALIVE_APP_FAILED, "Add keep alive applications failed. $."},
46     {EdmReturnErrCode::REPLACE_ADMIN_FAILED, "Replace admin failed. $."},
47     {EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED, "parameter verification failed. $"},
48     {EdmReturnErrCode::ENTERPRISE_POLICES_DENIED, "This function is prohibited by enterprise management policies."},
49 };
50 
CreateError(napi_env env,ErrCode errorCode)51 napi_value CreateError(napi_env env, ErrCode errorCode)
52 {
53     auto pair = GetMessageFromReturncode(errorCode);
54     return CreateError(env, pair.first, pair.second);
55 }
56 
CreateErrorWithUnknownCode(napi_env env,ErrCode errorCode)57 napi_value CreateErrorWithUnknownCode(napi_env env, ErrCode errorCode)
58 {
59     auto pair = GetMessageWithUnknownCodeFromReturncode(errorCode);
60     return CreateError(env, pair.first, pair.second);
61 }
62 
CreateErrorWithInnerCode(napi_env env,ErrCode errorCode,std::string & errMessage)63 napi_value CreateErrorWithInnerCode(napi_env env, ErrCode errorCode, std::string &errMessage)
64 {
65     auto pair = GetMessageFromReturncode(errorCode);
66     auto iter = pair.second.find("$");
67     if (iter != std::string::npos) {
68         pair.second = pair.second.replace(iter, 1, errMessage);
69     }
70     return CreateError(env, pair.first, pair.second);
71 }
72 
CreateError(napi_env env,int32_t errorCode,const std::string & errMessage)73 napi_value CreateError(napi_env env, int32_t errorCode, const std::string &errMessage)
74 {
75     napi_value result = nullptr;
76     napi_value message = nullptr;
77     napi_value errorCodeStr = nullptr;
78     napi_create_string_utf8(env, static_cast<char *>(std::to_string(errorCode).data()),
79         std::to_string(errorCode).size(), &errorCodeStr);
80     napi_create_string_utf8(env, errMessage.c_str(), errMessage.size(), &message);
81     napi_create_error(env, errorCodeStr, message, &result);
82     return result;
83 }
84 
GetMessageFromReturncode(ErrCode returnCode)85 std::pair<int32_t, std::string> GetMessageFromReturncode(ErrCode returnCode)
86 {
87     auto iter = errMessageMap.find(returnCode);
88     if (iter != errMessageMap.end()) {
89         return std::make_pair(returnCode, iter->second);
90     } else {
91         return std::make_pair(EdmReturnErrCode::PARAM_ERROR, "some thing wrong happend");
92     }
93 }
94 
GetMessageWithUnknownCodeFromReturncode(ErrCode returnCode)95 std::pair<int32_t, std::string> GetMessageWithUnknownCodeFromReturncode(ErrCode returnCode)
96 {
97     auto iter = errMessageMap.find(returnCode);
98     if (iter != errMessageMap.end()) {
99         return std::make_pair(returnCode, iter->second);
100     }
101     return std::make_pair(EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED, "parameter verification failed.");
102 }
103 } // namespace EDM
104 } // namespace OHOS