• 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 #include "uncaught_exception_callback.h"
16 
17 #include <string>
18 
19 #include "js_env_logger.h"
20 #include "native_engine/native_engine.h"
21 
22 namespace OHOS {
23 namespace JsEnv {
GetNativeStrFromJsTaggedObj(NativeObject * obj,const char * key)24 std::string UncaughtExceptionCallback::GetNativeStrFromJsTaggedObj(NativeObject* obj, const char* key)
25 {
26     if (obj == nullptr) {
27         JSENV_LOG_E("Failed to get value from key.");
28         return "";
29     }
30 
31     NativeValue* value = obj->GetProperty(key);
32     NativeString* valueStr = JsEnv::ConvertNativeValueTo<NativeString>(value);
33     if (valueStr == nullptr) {
34         JSENV_LOG_E("Failed to convert value from key.");
35         return "";
36     }
37 
38     size_t valueStrBufLength = valueStr->GetLength();
39     size_t valueStrLength = 0;
40     auto valueCStr = std::make_unique<char[]>(valueStrBufLength + 1);
41 
42     valueStr->GetCString(valueCStr.get(), valueStrBufLength + 1, &valueStrLength);
43     std::string ret(valueCStr.get(), valueStrLength);
44     JSENV_LOG_D("GetNativeStrFromJsTaggedObj Success.");
45     return ret;
46 }
47 
operator ()(NativeValue * value)48 void UncaughtExceptionCallback::operator()(NativeValue* value)
49 {
50     NativeObject* obj = JsEnv::ConvertNativeValueTo<NativeObject>(value);
51     std::string errorMsg = GetNativeStrFromJsTaggedObj(obj, "message");
52     std::string errorName = GetNativeStrFromJsTaggedObj(obj, "name");
53     std::string errorStack = GetNativeStrFromJsTaggedObj(obj, "stack");
54     std::string summary = "Error message:" + errorMsg + "\n";
55     const JsEnv::ErrorObject errorObj = {
56         .name = errorName,
57         .message = errorMsg,
58         .stack = errorStack
59     };
60     if (obj != nullptr && obj->HasProperty("code")) {
61         std::string errorCode = GetNativeStrFromJsTaggedObj(obj, "code");
62         summary += "Error code:" + errorCode + "\n";
63     }
64     if (errorStack.empty()) {
65         JSENV_LOG_E("errorStack is empty");
66         return;
67     }
68     auto errorPos = SourceMap::GetErrorPos(errorStack);
69     std::string error;
70     if (obj != nullptr) {
71         NativeValue* value = obj->GetProperty("errorfunc");
72         NativeFunction* fuc = JsEnv::ConvertNativeValueTo<NativeFunction>(value);
73         if (fuc != nullptr) {
74             error = fuc->GetSourceCodeInfo(errorPos);
75         }
76     }
77     if (sourceMapOperator_ == nullptr) {
78         JSENV_LOG_E("sourceMapOperator_ is empty");
79         return;
80     }
81     summary += error + "Stacktrace:\n" + sourceMapOperator_->TranslateBySourceMap(errorStack);
82     if (uncaughtTask_) {
83         uncaughtTask_(summary, errorObj);
84     }
85 }
86 } // namespace JsEnv
87 } // namespace OHOS
88