• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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, "the application does not have permission to call this function."},
24     {EdmReturnErrCode::SYSTEM_ABNORMALLY, "the system ability work abnormally."},
25     {EdmReturnErrCode::ENABLE_ADMIN_FAILED, "failed to enable the administrator application of the device."},
26     {EdmReturnErrCode::COMPONENT_INVALID, "the administrator ability component is invalid."},
27     {EdmReturnErrCode::ADMIN_INACTIVE, "the application is not a administrator of the device."},
28     {EdmReturnErrCode::DISABLE_ADMIN_FAILED, "failed to disable the administrator application of the device."},
29     {EdmReturnErrCode::UID_INVALID, "the specified user ID is invalid."},
30     {EdmReturnErrCode::INTERFACE_UNSUPPORTED, "the specified interface is not supported."},
31     {EdmReturnErrCode::PARAM_ERROR, "invalid input parameter."},
32     {EdmReturnErrCode::ADMIN_EDM_PERMISSION_DENIED,
33         "the administrator application does not have permission to manage the device."},
34     {EdmReturnErrCode::MANAGED_EVENTS_INVALID, "the specified managed event is invalid."},
35     {EdmReturnErrCode::SYSTEM_API_DENIED, "not system application."},
36     {EdmReturnErrCode::MANAGED_CERTIFICATE_FAILED, "manage certificate failed $."},
37     {EdmReturnErrCode::AUTHORIZE_PERMISSION_FAILED, "authorize permission to the application failed."},
38     {EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED, "a conflicting policy has been configured."},
39     {EdmReturnErrCode::APPLICATION_INSTALL_FAILED, "application install failed $."},
40     {EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED, "add os account failed."},
41 };
42 
CreateError(napi_env env,ErrCode errorCode)43 napi_value CreateError(napi_env env, ErrCode errorCode)
44 {
45     auto pair = GetMessageFromReturncode(errorCode);
46     return CreateError(env, pair.first, pair.second);
47 }
48 
CreateErrorWithInnerCode(napi_env env,ErrCode errorCode,std::string & errMessage)49 napi_value CreateErrorWithInnerCode(napi_env env, ErrCode errorCode, std::string &errMessage)
50 {
51     auto pair = GetMessageFromReturncode(errorCode);
52     auto iter = pair.second.find("$");
53     if (iter != std::string::npos) {
54         pair.second = pair.second.replace(iter, 1, errMessage);
55     }
56     return CreateError(env, pair.first, pair.second);
57 }
58 
CreateError(napi_env env,int32_t errorCode,const std::string & errMessage)59 napi_value CreateError(napi_env env, int32_t errorCode, const std::string &errMessage)
60 {
61     napi_value result = nullptr;
62     napi_value message = nullptr;
63     napi_value errorCodeStr = nullptr;
64     napi_create_string_utf8(env, static_cast<char *>(std::to_string(errorCode).data()),
65         std::to_string(errorCode).size(), &errorCodeStr);
66     napi_create_string_utf8(env, errMessage.c_str(), errMessage.size(), &message);
67     napi_create_error(env, errorCodeStr, message, &result);
68     return result;
69 }
70 
GetMessageFromReturncode(ErrCode returnCode)71 std::pair<int32_t, std::string> GetMessageFromReturncode(ErrCode returnCode)
72 {
73     auto iter = errMessageMap.find(returnCode);
74     if (iter != errMessageMap.end()) {
75         return std::make_pair(returnCode, iter->second);
76     } else {
77         return std::make_pair(EdmReturnErrCode::PARAM_ERROR, "some thing wrong happend");
78     }
79 }
80 } // namespace EDM
81 } // namespace OHOS