• 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 #include "ani_event_errors_utils.h"
16 
17 #include "event_log_wrapper.h"
18 #include "ability_business_error.h"
19 
20 namespace OHOS {
21 namespace EventManagerFwkAni {
ThrowError(ani_env * env,ani_object err)22 void ThrowError(ani_env *env, ani_object err)
23 {
24     if (env == nullptr) {
25         EVENT_LOGE("null env");
26         return;
27     }
28     env->ThrowError(static_cast<ani_error>(err));
29 }
30 
ThrowError(ani_env * env,int32_t errCode,const std::string & errorMsg)31 void ThrowError(ani_env *env, int32_t errCode, const std::string &errorMsg)
32 {
33     if (env == nullptr) {
34         EVENT_LOGE("null env");
35         return;
36     }
37     ThrowError(env, CreateError(env, errCode, errorMsg));
38 }
39 
ThrowError(ani_env * env,int32_t errCode)40 void ThrowError(ani_env *env, int32_t errCode)
41 {
42     if (env == nullptr) {
43         EVENT_LOGE("null env");
44         return;
45     }
46     ThrowError(env, errCode, FindErrorMsg(errCode));
47 }
48 
ThrowErrorByNativeError(ani_env * env,int32_t innerCode)49 void ThrowErrorByNativeError(ani_env *env, int32_t innerCode)
50 {
51     if (env == nullptr) {
52         EVENT_LOGE("null env");
53         return;
54     }
55     int32_t errCode = GetExternalCode(innerCode);
56     ThrowError(env, errCode, FindErrorMsg(errCode));
57 }
58 
WrapError(ani_env * env,const std::string & msg)59 ani_object WrapError(ani_env *env, const std::string &msg)
60 {
61     if (env == nullptr) {
62         EVENT_LOGE("null env");
63         return nullptr;
64     }
65     ani_status status = ANI_ERROR;
66     ani_string aniMsg = nullptr;
67     if ((status = env->String_NewUTF8(msg.c_str(), msg.size(), &aniMsg)) != ANI_OK) {
68         EVENT_LOGE("String_NewUTF8 failed %{public}d", status);
69         return nullptr;
70     }
71     ani_ref undefRef;
72     if ((status = env->GetUndefined(&undefRef)) != ANI_OK) {
73         EVENT_LOGE("GetUndefined failed %{public}d", status);
74         return nullptr;
75     }
76     ani_class cls = nullptr;
77     if ((status = env->FindClass(ERROR_CLASS_NAME, &cls)) != ANI_OK) {
78         EVENT_LOGE("FindClass failed %{public}d", status);
79         return nullptr;
80     }
81     ani_method method = nullptr;
82     if ((status = env->Class_FindMethod(cls, "<ctor>", "Lstd/core/String;Lescompat/ErrorOptions;:V", &method)) !=
83         ANI_OK) {
84         EVENT_LOGE("Class_FindMethod failed %{public}d", status);
85         return nullptr;
86     }
87     ani_object obj = nullptr;
88     if ((status = env->Object_New(cls, method, &obj, aniMsg, undefRef)) != ANI_OK) {
89         EVENT_LOGE("Object_New failed %{public}d", status);
90         return nullptr;
91     }
92     return obj;
93 }
94 
CreateError(ani_env * env,int32_t code,const std::string & msg)95 ani_object CreateError(ani_env *env, int32_t code, const std::string &msg)
96 {
97     if (env == nullptr) {
98         EVENT_LOGE("null env");
99         return nullptr;
100     }
101     ani_status status = ANI_ERROR;
102     ani_class cls = nullptr;
103     if ((status = env->FindClass(BUSINESS_ERROR_CLASS, &cls)) != ANI_OK) {
104         EVENT_LOGE("FindClass failed %{public}d", status);
105         return nullptr;
106     }
107     ani_method method = nullptr;
108     if ((status = env->Class_FindMethod(cls, "<ctor>", "DLescompat/Error;:V", &method)) != ANI_OK) {
109         EVENT_LOGE("Class_FindMethod failed %{public}d", status);
110         return nullptr;
111     }
112     ani_object error = WrapError(env, msg);
113     if (error == nullptr) {
114         EVENT_LOGE("error nulll");
115         return nullptr;
116     }
117     ani_object obj = nullptr;
118     ani_double iCode = static_cast<ani_double>(code);
119     if ((status = env->Object_New(cls, method, &obj, iCode, error)) != ANI_OK) {
120         EVENT_LOGE("Object_New failed %{public}d", status);
121         return nullptr;
122     }
123     return obj;
124 }
125 
GetExternalCode(int32_t errCode)126 int32_t GetExternalCode(int32_t errCode)
127 {
128     int32_t externalCode = static_cast<int32_t>(AbilityRuntime::GetJsErrorCodeByNativeError(errCode));
129     EVENT_LOGD("GetExternalCode %{public}d to %{public}d", errCode, externalCode);
130     return externalCode;
131 }
132 
FindErrorMsg(const int32_t errCode)133 std::string FindErrorMsg(const int32_t errCode)
134 {
135     auto findMsg = ERR_CODE_MAP.find(errCode);
136     if (findMsg == ERR_CODE_MAP.end()) {
137         EVENT_LOGE("FindAnsErrMsg Inner error.");
138         return "Inner error.";
139     }
140     return findMsg->second;
141 }
142 } //namespace EventManagerFwkAni
143 } //namespace OHOS