• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ans_inner_errors.h"
17 
18 #include "ans_log_wrapper.h"
19 #include "ipc_types.h"
20 
21 namespace OHOS {
22 namespace Notification {
23 static const std::unordered_map<int32_t, std::string> ANS_ERROR_CODE_MESSAGE_MAP = {
24     {ERROR_PERMISSION_DENIED, "Permission denied"},
25     {ERROR_NOT_SYSTEM_APP, "Not system application to call the interface"},
26     {ERROR_PARAM_INVALID, "Invalid parameter"},
27     {ERROR_SYSTEM_CAP_ERROR, "SystemCapability not found"},
28     {ERROR_INTERNAL_ERROR, "Internal error. Possible cause: 1.IPC communication failed. 2.Memory operation error"},
29     {ERROR_IPC_ERROR, "Marshalling or unmarshalling error"},
30     {ERROR_SERVICE_CONNECT_ERROR, "Failed to connect to the service"},
31     {ERROR_NOTIFICATION_CLOSED, "Notification disabled"},
32     {ERROR_SLOT_CLOSED, "Notification slot disabled"},
33     {ERROR_NOTIFICATION_UNREMOVABLE, "Notification deletion disabled"},
34     {ERROR_NOTIFICATION_NOT_EXIST, "The notification does not exist"},
35     {ERROR_USER_NOT_EXIST, "The user does not exist"},
36     {ERROR_OVER_MAX_NUM_PER_SECOND, "The notification sending frequency reaches the upper limit"},
37     {ERROR_DISTRIBUTED_OPERATION_FAILED, "Distributed operation failed"},
38     {ERROR_READ_TEMPLATE_CONFIG_FAILED, "Failed to read the template configuration"},
39     {ERROR_NO_MEMORY, "Memory operation failed"},
40     {ERROR_BUNDLE_NOT_FOUND, "The specified bundle name was not found"},
41     {ERROR_NO_AGENT_SETTING, "There is no corresponding agent relationship configuration"},
42     {ERROR_DIALOG_IS_POPPING, "Dialog is popping"},
43     {ERROR_SETTING_WINDOW_EXIST, "The notification settings window is already displayed"},
44     {ERROR_NO_PROFILE_TEMPLATE, "Not exit noNotDisturb profile template"},
45     {ERROR_REPEAT_SET, "Repeat create or end"},
46     {ERROR_NO_RIGHT, "The specified process does not have the permission"},
47     {ERROR_EXPIRED_NOTIFICATION, "Low update version"},
48     {ERROR_NETWORK_UNREACHABLE, "Network unreachable"},
49     {ERROR_REJECTED_WITH_DISABLE_NOTIFICATION,
50         "The application is not allowed to send notifications due to permission settings"},
51     {ERROR_DISTRIBUTED_OPERATION_TIMEOUT, "Distributed operation timeout"},
52 };
53 
54 static std::vector<std::pair<uint32_t, int32_t>> errorsConvert = {
55     {ERR_ANS_PERMISSION_DENIED, ERROR_PERMISSION_DENIED},
56     {ERR_ANS_NON_SYSTEM_APP, ERROR_NOT_SYSTEM_APP},
57     {ERR_ANS_NOT_SYSTEM_SERVICE, ERROR_NOT_SYSTEM_APP},
58     {ERR_ANS_INVALID_PARAM, ERROR_PARAM_INVALID},
59     {ERR_ANS_INVALID_UID, ERROR_PARAM_INVALID},
60     {ERR_ANS_ICON_OVER_SIZE, ERROR_PARAM_INVALID},
61     {ERR_ANS_PICTURE_OVER_SIZE, ERROR_PARAM_INVALID},
62     {ERR_ANS_PUSH_CHECK_EXTRAINFO_INVALID, ERROR_PARAM_INVALID},
63     {ERR_ANS_NO_MEMORY, ERROR_NO_MEMORY},
64     {ERR_ANS_TASK_ERR, ERROR_INTERNAL_ERROR},
65     {ERR_ANS_PARCELABLE_FAILED, ERROR_IPC_ERROR},
66     {ERR_ANS_TRANSACT_FAILED, ERROR_IPC_ERROR},
67     {ERR_ANS_REMOTE_DEAD, ERROR_IPC_ERROR},
68     {ERR_INVALID_VALUE, ERROR_IPC_ERROR},
69     {ERR_INVALID_DATA, ERROR_IPC_ERROR},
70     {DEAD_OBJECT, ERROR_IPC_ERROR},
71     {ERR_ANS_SERVICE_NOT_READY, ERROR_SERVICE_CONNECT_ERROR},
72     {ERR_ANS_SERVICE_NOT_CONNECTED, ERROR_SERVICE_CONNECT_ERROR},
73     {ERR_ANS_NOT_ALLOWED, ERROR_NOTIFICATION_CLOSED},
74     {ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED, ERROR_SLOT_CLOSED},
75     {ERR_ANS_NOTIFICATION_IS_UNREMOVABLE, ERROR_NOTIFICATION_UNREMOVABLE},
76     {ERR_ANS_NOTIFICATION_NOT_EXISTS, ERROR_NOTIFICATION_NOT_EXIST},
77     {ERR_ANS_GET_ACTIVE_USER_FAILED, ERROR_USER_NOT_EXIST},
78     {ERR_ANS_INVALID_PID, ERROR_BUNDLE_NOT_FOUND},
79     {ERR_ANS_INVALID_BUNDLE, ERROR_BUNDLE_NOT_FOUND},
80     {ERR_ANS_OVER_MAX_ACTIVE_PERSECOND, ERROR_OVER_MAX_NUM_PER_SECOND},
81     {ERR_ANS_OVER_MAX_UPDATE_PERSECOND, ERROR_OVER_MAX_NUM_PER_SECOND},
82     {ERR_ANS_DISTRIBUTED_OPERATION_FAILED, ERROR_DISTRIBUTED_OPERATION_FAILED},
83     {ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, ERROR_DISTRIBUTED_OPERATION_FAILED},
84     {ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, ERROR_READ_TEMPLATE_CONFIG_FAILED},
85     {ERR_ANS_REPEAT_CREATE, ERROR_REPEAT_SET},
86     {ERR_ANS_END_NOTIFICATION, ERROR_REPEAT_SET},
87     {ERR_ANS_EXPIRED_NOTIFICATION, ERROR_EXPIRED_NOTIFICATION},
88     {ERR_ANS_PUSH_CHECK_FAILED, ERROR_NO_RIGHT},
89     {ERR_ANS_PUSH_CHECK_UNREGISTERED, ERROR_NO_RIGHT},
90     {ERR_ANS_LOCAL_SUBSCRIBE_CHECK_FAILED, ERROR_NO_RIGHT},
91     {ERR_ANS_PUSH_CHECK_NETWORK_UNREACHABLE, ERROR_NETWORK_UNREACHABLE},
92     {ERR_ANS_NO_AGENT_SETTING, ERROR_NO_AGENT_SETTING},
93     {ERR_ANS_DIALOG_IS_POPPING, ERROR_DIALOG_IS_POPPING},
94     {ERR_ANS_NO_PROFILE_TEMPLATE, ERROR_NO_PROFILE_TEMPLATE},
95     {ERR_ANS_REJECTED_WITH_DISABLE_NOTIFICATION, ERROR_REJECTED_WITH_DISABLE_NOTIFICATION},
96     {ERR_ANS_OPERATION_TIMEOUT, ERROR_DISTRIBUTED_OPERATION_TIMEOUT},
97 };
98 
ErrorToExternal(uint32_t errCode)99 int32_t ErrorToExternal(uint32_t errCode)
100 {
101     int32_t ExternalCode = ERROR_INTERNAL_ERROR;
102     for (const auto &errorConvert : errorsConvert) {
103         if (errCode == errorConvert.first) {
104             ExternalCode = errorConvert.second;
105             break;
106         }
107     }
108 
109     ANS_LOGI("internal error[%{public}u] to [%{public}d]", errCode, ExternalCode);
110     return ExternalCode;
111 }
112 
GetAnsErrMessage(uint32_t errCode,std::string defaultMsg)113 std::string GetAnsErrMessage(uint32_t errCode, std::string defaultMsg)
114 {
115     auto iter = ANS_ERROR_CODE_MESSAGE_MAP.find(errCode);
116     return iter != ANS_ERROR_CODE_MESSAGE_MAP.end() ? iter->second : defaultMsg;
117 }
118 }  // namespace Notification
119 }  // namespace OHOS
120