• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef ERROR_HANDLER_H
17 #define ERROR_HANDLER_H
18 
19 #include "ani.h"
20 #include "hilog/log.h"
21 #include <string>
22 #include <cstdint>
23 
24 namespace OHOS::uitest {
25     using namespace nlohmann;
26     using namespace std;
27     using namespace OHOS::HiviewDFX;
28     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LogType::LOG_CORE, 0xD003100, "UiTestKit"};
29 
30     constexpr const char* BUSINESS_ERROR_CLASS = "L@ohos/base/BusinessError;";
31     class ErrorHandler {
32     public:
Throw(ani_env * env,int32_t code,const string & errMsg)33         static ani_status Throw(ani_env *env, int32_t code, const string &errMsg)
34         {
35             return Throw(env, BUSINESS_ERROR_CLASS, code, errMsg);
36         }
37     private:
WrapError(ani_env * env,const std::string & msg)38         static ani_object WrapError(ani_env *env, const std::string &msg)
39         {
40             if (env == nullptr) {
41                 return nullptr;
42             }
43             ani_class cls = nullptr;
44             ani_method method = nullptr;
45             ani_object obj = nullptr;
46             ani_string aniMsg = nullptr;
47             if (env->String_NewUTF8(msg.c_str(), msg.size(), &aniMsg) != ANI_OK) {
48                 HiLog::Error(LABEL, "StringToAniStr failed");
49                 return nullptr;
50             }
51             ani_ref undefRef;
52             env->GetUndefined(&undefRef);
53             ani_status status = env->FindClass("Lescompat/Error;", &cls);
54             if (status != ANI_OK) {
55                 HiLog::Error(LABEL, "FindClass : %{public}d", status);
56                 return nullptr;
57             }
58             status = env->Class_FindMethod(cls, "<ctor>", "Lstd/core/String;Lescompat/ErrorOptions;:V", &method);
59             if (status != ANI_OK) {
60                 HiLog::Error(LABEL, "Class_FindMethod : %{public}d", status);
61                 return nullptr;
62             }
63             status = env->Object_New(cls, method, &obj, aniMsg, undefRef);
64             if (status != ANI_OK) {
65                 HiLog::Error(LABEL, "Object_New : %{public}d", status);
66                 return nullptr;
67             }
68             return obj;
69         }
70 
Throw(ani_env * env,const char * className,int32_t code,const string & errMsg)71         static ani_status Throw(ani_env *env, const char *className, int32_t code, const string &errMsg)
72         {
73             if (env == nullptr) {
74                 HiLog::Error(LABEL, "Invalid env");
75                 return ANI_INVALID_ARGS;
76             }
77             ani_class cls;
78             if (ANI_OK != env->FindClass(className, &cls)) {
79                 HiLog::Error(LABEL, "Not found class BusinessError");
80                 return ANI_ERROR;
81             }
82             ani_method method;
83             if (ANI_OK != env->Class_FindMethod(cls, "<ctor>", "DLescompat/Error;:V", &method)) {
84                 HiLog::Error(LABEL, "Not found method of BusinessError");
85                 return ANI_ERROR;
86             }
87             ani_object error = WrapError(env, errMsg);
88             if (error == nullptr) {
89                 HiLog::Error(LABEL, "WrapError failed");
90                 return ANI_ERROR;
91             }
92             ani_object obj;
93             ani_double dCode(code);
94             if (env->Object_New(cls, method, &obj, dCode, error) != ANI_OK) {
95                 HiLog::Error(LABEL, "Object_New error fail");
96                 return ANI_ERROR;
97             }
98             return env->ThrowError(static_cast<ani_error>(obj));
99         }
100     };
101 }
102 
103 #endif