Skip to content

Commit f8c0c19

Browse files
committed
add dom callbacks error handling; add dom commands error handling
1 parent 1da865b commit f8c0c19

File tree

6 files changed

+116
-59
lines changed

6 files changed

+116
-59
lines changed

runtime/src/main/jni/DOMDomainCallbackHandlers.cpp

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Created by pkanev on 5/10/2017.
33
//
44

5+
#include <sstream>
56
#include <ArgConverter.h>
67
#include <NativeScriptAssert.h>
78
#include "DOMDomainCallbackHandlers.h"
@@ -19,65 +20,83 @@ void DOMDomainCallbackHandlers::DocumentUpdatedCallback(const v8::FunctionCallba
1920
}
2021

2122
void DOMDomainCallbackHandlers::ChildNodeInsertedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
22-
auto domAgentInstance = V8DOMAgentImpl::Instance;
23-
24-
if (!domAgentInstance) {
25-
return;
26-
}
27-
28-
if (args.Length() == 0) {
29-
return;
30-
}
31-
32-
auto isolate = args.GetIsolate();
33-
34-
v8::HandleScope scope(isolate);
35-
36-
auto context = isolate->GetCurrentContext();
37-
38-
// TODO: Pete: Validate!
39-
auto parentId = args[0]->ToNumber(isolate);
40-
auto lastId = args[1]->ToNumber(isolate);
41-
auto node = args[2]->ToString(isolate);
42-
43-
auto nodeString = ArgConverter::ConvertToString(node);
44-
auto nodeCStr = nodeString.c_str();
45-
auto nodeJson = protocol::parseJSON(nodeCStr);
46-
47-
protocol::ErrorSupport errorSupport;
48-
auto domNode = protocol::DOM::Node::parse(nodeJson.get(), &errorSupport);
49-
50-
auto errorSupportString = errorSupport.errors().utf8();
51-
if (!errorSupportString.empty()) {
52-
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
53-
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
23+
try {
24+
auto domAgentInstance = V8DOMAgentImpl::Instance;
25+
26+
if (!domAgentInstance) {
27+
return;
28+
}
29+
30+
auto isolate = args.GetIsolate();
31+
32+
v8::HandleScope scope(isolate);
33+
34+
if (args.Length() != 3 || !(args[0]->IsNumber() && args[1]->IsNumber() && args[2]->IsString())) {
35+
throw NativeScriptException("Calling ChildNodeInserted with invalid arguments. Required params: parentId: number, lastId: number, node: JSON String");
36+
}
37+
38+
auto parentId = args[0]->ToNumber(isolate);
39+
auto lastId = args[1]->ToNumber(isolate);
40+
auto node = args[2]->ToString(isolate);
41+
42+
auto nodeString = ArgConverter::ConvertToString(node);
43+
auto nodeCStr = nodeString.c_str();
44+
auto nodeJson = protocol::parseJSON(nodeCStr);
45+
46+
protocol::ErrorSupport errorSupport;
47+
auto domNode = protocol::DOM::Node::parse(nodeJson.get(), &errorSupport);
48+
49+
auto errorSupportString = errorSupport.errors().utf8();
50+
if (!errorSupportString.empty()) {
51+
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
52+
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
53+
}
54+
55+
domAgentInstance->m_frontend.childNodeInserted(parentId->Int32Value(), lastId->Int32Value(), std::move(domNode));
56+
} catch (NativeScriptException& e) {
57+
e.ReThrowToV8();
58+
} catch (std::exception e) {
59+
std::stringstream ss;
60+
ss << "Error: c exception: " << e.what() << std::endl;
61+
NativeScriptException nsEx(ss.str());
62+
nsEx.ReThrowToV8();
63+
} catch (...) {
64+
NativeScriptException nsEx(std::string("Error: c exception!"));
65+
nsEx.ReThrowToV8();
5466
}
55-
56-
domAgentInstance->m_frontend.childNodeInserted(parentId->Int32Value(), lastId->Int32Value(), std::move(domNode));
5767
}
5868

5969
void DOMDomainCallbackHandlers::ChildNodeRemovedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
60-
auto domAgentInstance = V8DOMAgentImpl::Instance;
61-
62-
if (!domAgentInstance) {
63-
return;
64-
}
65-
66-
if (args.Length() == 0) {
67-
return;
70+
try {
71+
auto domAgentInstance = V8DOMAgentImpl::Instance;
72+
73+
if (!domAgentInstance) {
74+
return;
75+
}
76+
77+
auto isolate = args.GetIsolate();
78+
79+
v8::HandleScope scope(isolate);
80+
81+
if (args.Length() != 2 || !(args[0]->IsNumber() && args[1]->IsNumber())) {
82+
throw NativeScriptException("Calling ChildNodeRemoved with invalid arguments. Required params: parentId: number, nodeId: number");
83+
}
84+
85+
auto parentId = args[0]->ToNumber(isolate);
86+
auto nodeId = args[1]->ToNumber(isolate);
87+
88+
domAgentInstance->m_frontend.childNodeRemoved(parentId->Int32Value(), nodeId->Int32Value());
89+
} catch (NativeScriptException& e) {
90+
e.ReThrowToV8();
91+
} catch (std::exception e) {
92+
std::stringstream ss;
93+
ss << "Error: c exception: " << e.what() << std::endl;
94+
NativeScriptException nsEx(ss.str());
95+
nsEx.ReThrowToV8();
96+
} catch (...) {
97+
NativeScriptException nsEx(std::string("Error: c exception!"));
98+
nsEx.ReThrowToV8();
6899
}
69-
70-
auto isolate = args.GetIsolate();
71-
72-
v8::HandleScope scope(isolate);
73-
74-
auto context = isolate->GetCurrentContext();
75-
76-
// TODO: Pete: Validate!
77-
auto parentId = args[0]->ToNumber(isolate);
78-
auto nodeId = args[1]->ToNumber(isolate);
79-
80-
domAgentInstance->m_frontend.childNodeRemoved(parentId->Int32Value(), nodeId->Int32Value());
81100
}
82101

83102
void DOMDomainCallbackHandlers::AttributeModifiedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {

runtime/src/main/jni/DOMDomainCallbackHandlers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <include/v8.h>
99
#include <v8_inspector/src/inspector/v8-dom-agent-impl.h>
1010
#include "JsV8InspectorClient.h"
11+
#include "NativeScriptException.h"
1112

1213
namespace tns {
1314
class DOMDomainCallbackHandlers {

runtime/src/main/jni/v8_inspector/src/inspector/utils/v8-inspector-common.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <v8_inspector/src/inspector/utils/v8-inspector-common.h>
66
#include <ArgConverter.h>
7+
#include <NativeScriptAssert.h>
78

89
using tns::ArgConverter;
910

@@ -25,5 +26,13 @@ namespace v8_inspector {
2526

2627
return v8::Local<v8::Object>();
2728
}
29+
30+
std::string Common::getJSCallErrorMessage(const std::string &functionName, v8::Local<v8::String> tcMessage) {
31+
auto errorMessage = "Error thrown while calling " + functionName + ": " + ArgConverter::ConvertToString(tcMessage);
32+
33+
DEBUG_WRITE_FORCE("JS Error: %s", errorMessage.c_str());
34+
35+
return errorMessage;
36+
}
2837
}
2938
}

runtime/src/main/jni/v8_inspector/src/inspector/utils/v8-inspector-common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace v8_inspector {
1212
class Common {
1313
public:
1414
static v8::Local<v8::Object> getGlobalInspectorObject(v8::Isolate *isolate);
15+
static std::string getJSCallErrorMessage(const std::string& functionName, v8::Local<v8::String> tcMessage);
1516
};
1617
}
1718
}

runtime/src/main/jni/v8_inspector/src/inspector/v8-css-agent-impl.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ namespace v8_inspector {
140140
*out_attributesStyle = Maybe<protocol::CSS::CSSStyle>(std::move(attributeStyle));
141141
}
142142

143-
void V8CSSAgentImpl::getComputedStyleForNode(ErrorString *, int in_nodeId,
143+
void V8CSSAgentImpl::getComputedStyleForNode(ErrorString *errorString, int in_nodeId,
144144
std::unique_ptr<protocol::Array<protocol::CSS::CSSComputedStyleProperty>> *out_computedStyle) {
145145
auto computedStylePropertyArr = protocol::Array<protocol::CSS::CSSComputedStyleProperty>::create();
146146

147-
auto getComputedStylesForNodeString = "getComputedStylesForNode";
147+
std::string getComputedStylesForNodeString = "getComputedStylesForNode";
148148
// TODO: Pete: Find a better way to get a hold of the isolate
149149
auto isolate = v8::Isolate::GetCurrent();
150150
auto context = isolate->GetCurrentContext();
@@ -158,7 +158,17 @@ namespace v8_inspector {
158158
if (!getComputedStylesForNode.IsEmpty() && getComputedStylesForNode->IsFunction()) {
159159
auto getComputedStylesForNodeFunc = getComputedStylesForNode.As<v8::Function>();
160160
v8::Local<v8::Value> args[] = { v8::Number::New(isolate, in_nodeId) };
161+
v8::TryCatch tc;
162+
161163
auto maybeResult = getComputedStylesForNodeFunc->Call(context, global, 1, args);
164+
165+
if (tc.HasCaught()) {
166+
*errorString = utils::Common::getJSCallErrorMessage(getComputedStylesForNodeString, tc.Message()->Get()).c_str();
167+
168+
*out_computedStyle = std::move(computedStylePropertyArr);
169+
return;
170+
}
171+
162172
v8::Local<v8::Value> outResult;
163173

164174
if (maybeResult.ToLocal(&outResult)) {

runtime/src/main/jni/v8_inspector/src/inspector/v8-dom-agent-impl.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace v8_inspector {
5858
.setNodeValue("")
5959
.build();
6060

61-
auto getDocumentFunctionString = "getDocument";
61+
std::string getDocumentFunctionString = "getDocument";
6262
// TODO: Pete: Find a better way to get a hold of the isolate
6363
auto isolate = v8::Isolate::GetCurrent();
6464
auto context = isolate->GetCurrentContext();
@@ -72,7 +72,17 @@ namespace v8_inspector {
7272
if (!getDocument.IsEmpty() && getDocument->IsFunction()) {
7373
auto getDocumentFunc = getDocument.As<v8::Function>();
7474
v8::Local<v8::Value> args[] = { };
75+
v8::TryCatch tc;
76+
7577
auto maybeResult = getDocumentFunc->Call(context, global, 0, args);
78+
79+
if (tc.HasCaught()) {
80+
*errorString = utils::Common::getJSCallErrorMessage(getDocumentFunctionString, tc.Message()->Get()).c_str();
81+
82+
*out_root = std::move(defaultNode);
83+
return;
84+
}
85+
7686
v8::Local<v8::Value> outResult;
7787

7888
if (maybeResult.ToLocal(&outResult)) {
@@ -87,7 +97,7 @@ namespace v8_inspector {
8797
*errorString = errorSupportString.c_str();
8898
if (!errorSupportString.empty()) {
8999
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
90-
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
100+
DEBUG_WRITE_FORCE("JS Error: %s", errorMessage, errorSupportString.c_str());
91101
} else {
92102
*out_root = std::move(domNode);
93103

@@ -103,7 +113,8 @@ namespace v8_inspector {
103113
}
104114

105115
void V8DOMAgentImpl::removeNode(ErrorString *errorString, int in_nodeId) {
106-
auto removeNodeFunctionString = "removeNode";
116+
std::string removeNodeFunctionString = "removeNode";
117+
107118
// TODO: Pete: Find a better way to get a hold of the isolate
108119
auto isolate = v8::Isolate::GetCurrent();
109120
auto context = isolate->GetCurrentContext();
@@ -117,8 +128,14 @@ namespace v8_inspector {
117128
if (!removeNode.IsEmpty() && removeNode->IsFunction()) {
118129
auto removeNodeFunc = removeNode.As<v8::Function>();
119130
v8::Local<v8::Value> args[] = { v8::Number::New(isolate, in_nodeId) };
131+
v8::TryCatch tc;
132+
120133
removeNodeFunc->Call(context, global, 1, args);
121134

135+
if (tc.HasCaught()) {
136+
*errorString = utils::Common::getJSCallErrorMessage(removeNodeFunctionString, tc.Message()->Get()).c_str();
137+
}
138+
122139
return;
123140
}
124141
}

0 commit comments

Comments
 (0)