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