Skip to content

Commit 7c62581

Browse files
ryuhei shimaryuhei shima
authored andcommitted
inspector: return errors when CDP protocol event emission fails
1 parent 3725bd2 commit 7c62581

File tree

9 files changed

+659
-20
lines changed

9 files changed

+659
-20
lines changed

doc/api/inspector.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ added:
520520
-->
521521

522522
* `params` {Object}
523+
* Returns: {Array} An array of errors from each session
523524

524525
This feature is only available with the `--experimental-network-inspection` flag enabled.
525526

@@ -537,6 +538,7 @@ added:
537538
-->
538539

539540
* `params` {Object}
541+
* Returns: {Array} An array of errors from each session
540542

541543
This feature is only available with the `--experimental-network-inspection` flag enabled.
542544

@@ -551,6 +553,7 @@ added:
551553
-->
552554

553555
* `params` {Object}
556+
* Returns: {Array} An array of errors from each session
554557

555558
This feature is only available with the `--experimental-network-inspection` flag enabled.
556559

@@ -566,6 +569,7 @@ added:
566569
-->
567570

568571
* `params` {Object}
572+
* Returns: {Array} An array of errors from each session
569573

570574
This feature is only available with the `--experimental-network-inspection` flag enabled.
571575

@@ -581,6 +585,7 @@ added:
581585
-->
582586

583587
* `params` {Object}
588+
* Returns: {Array} An array of errors from each session
584589

585590
This feature is only available with the `--experimental-network-inspection` flag enabled.
586591

@@ -596,6 +601,7 @@ added:
596601
-->
597602

598603
* `params` {Object}
604+
* Returns: {Array} An array of errors from each session
599605

600606
This feature is only available with the `--experimental-network-inspection` flag enabled.
601607

@@ -610,6 +616,7 @@ added:
610616
-->
611617

612618
* `params` {Object}
619+
* Returns: {Array} An array of errors from each session
613620

614621
This feature is only available with the `--experimental-network-inspection` flag enabled.
615622

@@ -624,6 +631,7 @@ added:
624631
-->
625632

626633
* `params` {Object}
634+
* Returns: {Array} An array of errors from each session
627635

628636
This feature is only available with the `--experimental-network-inspection` flag enabled.
629637

@@ -638,6 +646,7 @@ added:
638646
-->
639647

640648
* `params` {Object}
649+
* Returns: {Array} An array of errors from each session
641650

642651
This feature is only available with the `--experimental-network-inspection` flag enabled.
643652

@@ -696,6 +705,7 @@ added:
696705
* `isLocalStorage` {boolean}
697706
* `key` {string}
698707
* `newValue` {string}
708+
* Returns: {Array} An array of errors from each session
699709

700710
This feature is only available with the
701711
`--experimental-storage-inspection` flag enabled.
@@ -716,6 +726,7 @@ added:
716726
* `storageKey` {string}
717727
* `isLocalStorage` {boolean}
718728
* `key` {string}
729+
* Returns: {Array} An array of errors from each session
719730

720731
This feature is only available with the
721732
`--experimental-storage-inspection` flag enabled.
@@ -738,6 +749,7 @@ added:
738749
* `key` {string}
739750
* `oldValue` {string}
740751
* `newValue` {string}
752+
* Returns: {Array} An array of errors from each session
741753

742754
This feature is only available with the
743755
`--experimental-storage-inspection` flag enabled.
@@ -757,6 +769,7 @@ added:
757769
* `securityOrigin` {string}
758770
* `storageKey` {string}
759771
* `isLocalStorage` {boolean}
772+
* Returns: {Array} An array of errors from each session
760773

761774
This feature is only available with the
762775
`--experimental-storage-inspection` flag enabled.
@@ -775,6 +788,7 @@ added:
775788
* `params` {Object}
776789
* `isLocalStorage` {boolean}
777790
* `storageMap` {Object}
791+
* Returns: {Array} An array of errors from each session
778792

779793
This feature is only available with the
780794
`--experimental-storage-inspection` flag enabled.

lib/inspector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function inspectorWaitForDebugger() {
209209
function broadcastToFrontend(eventName, params = kEmptyObject) {
210210
validateString(eventName, 'eventName');
211211
validateObject(params, 'params');
212-
emitProtocolEvent(eventName, params);
212+
return emitProtocolEvent(eventName, params);
213213
}
214214

215215
const Network = {

src/inspector/dom_storage_agent.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@ using v8::Local;
1414
using v8::Object;
1515
using v8::Value;
1616

17+
static void ThrowEventError(v8::Isolate* isolate, const std::string& message) {
18+
isolate->ThrowException(v8::Exception::Error(
19+
v8::String::NewFromUtf8(isolate, message.c_str()).ToLocalChecked()));
20+
}
21+
1722
std::unique_ptr<protocol::DOMStorage::StorageId> createStorageIdFromObject(
1823
Local<Context> context, Local<Object> storage_id_obj) {
1924
protocol::String security_origin;
25+
Isolate* isolate = Isolate::GetCurrent();
2026
if (!ObjectGetProtocolString(context, storage_id_obj, "securityOrigin")
2127
.To(&security_origin)) {
28+
ThrowEventError(isolate, "Missing securityOrigin in storageId");
2229
return {};
2330
}
2431
bool is_local_storage =
2532
ObjectGetBool(context, storage_id_obj, "isLocalStorage").FromMaybe(false);
2633
protocol::String storageKey;
2734
if (!ObjectGetProtocolString(context, storage_id_obj, "storageKey")
2835
.To(&storageKey)) {
36+
ThrowEventError(isolate, "Missing storageKey in storageId");
2937
return {};
3038
}
3139

@@ -119,8 +127,10 @@ protocol::DispatchResponse DOMStorageAgent::clear(
119127

120128
void DOMStorageAgent::domStorageItemAdded(Local<Context> context,
121129
Local<Object> params) {
130+
Isolate* isolate = env_->isolate();
122131
Local<Object> storage_id_obj;
123132
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
133+
ThrowEventError(isolate, "Missing storageId in event");
124134
return;
125135
}
126136

@@ -132,19 +142,23 @@ void DOMStorageAgent::domStorageItemAdded(Local<Context> context,
132142

133143
protocol::String key;
134144
if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
145+
ThrowEventError(isolate, "Missing key in event");
135146
return;
136147
}
137148
protocol::String new_value;
138149
if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) {
150+
ThrowEventError(isolate, "Missing newValue in event");
139151
return;
140152
}
141153
frontend_->domStorageItemAdded(std::move(storage_id), key, new_value);
142154
}
143155

144156
void DOMStorageAgent::domStorageItemRemoved(Local<Context> context,
145157
Local<Object> params) {
158+
Isolate* isolate = env_->isolate();
146159
Local<Object> storage_id_obj;
147160
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
161+
ThrowEventError(isolate, "Missing storageId in event");
148162
return;
149163
}
150164
std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
@@ -156,15 +170,18 @@ void DOMStorageAgent::domStorageItemRemoved(Local<Context> context,
156170

157171
protocol::String key;
158172
if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
173+
ThrowEventError(isolate, "Missing key in event");
159174
return;
160175
}
161176
frontend_->domStorageItemRemoved(std::move(storage_id), key);
162177
}
163178

164179
void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
165180
Local<Object> params) {
181+
Isolate* isolate = env_->isolate();
166182
Local<Object> storage_id_obj;
167183
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
184+
ThrowEventError(isolate, "Missing storageId in event");
168185
return;
169186
}
170187

@@ -177,14 +194,17 @@ void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
177194

178195
protocol::String key;
179196
if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
197+
ThrowEventError(isolate, "Missing key in event");
180198
return;
181199
}
182200
protocol::String old_value;
183201
if (!ObjectGetProtocolString(context, params, "oldValue").To(&old_value)) {
202+
ThrowEventError(isolate, "Missing oldValue in event");
184203
return;
185204
}
186205
protocol::String new_value;
187206
if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) {
207+
ThrowEventError(isolate, "Missing newValue in event");
188208
return;
189209
}
190210
frontend_->domStorageItemUpdated(
@@ -193,8 +213,10 @@ void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
193213

194214
void DOMStorageAgent::domStorageItemsCleared(Local<Context> context,
195215
Local<Object> params) {
216+
Isolate* isolate = env_->isolate();
196217
Local<Object> storage_id_obj;
197218
if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
219+
ThrowEventError(isolate, "Missing storageId in event");
198220
return;
199221
}
200222
std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
@@ -212,27 +234,32 @@ void DOMStorageAgent::registerStorage(Local<Context> context,
212234
HandleScope handle_scope(isolate);
213235
bool is_local_storage;
214236
if (!ObjectGetBool(context, params, "isLocalStorage").To(&is_local_storage)) {
237+
ThrowEventError(isolate, "Missing isLocalStorage in event");
215238
return;
216239
}
217240
Local<Object> storage_map_obj;
218241
if (!ObjectGetObject(context, params, "storageMap")
219242
.ToLocal(&storage_map_obj)) {
243+
ThrowEventError(isolate, "Missing storageMap in event");
220244
return;
221245
}
222246
std::unordered_map<std::string, std::string>& storage_map =
223247
is_local_storage ? local_storage_map_ : session_storage_map_;
224248
Local<Array> property_names;
225249
if (!storage_map_obj->GetOwnPropertyNames(context).ToLocal(&property_names)) {
250+
ThrowEventError(isolate, "Failed to get property names from storageMap");
226251
return;
227252
}
228253
uint32_t length = property_names->Length();
229254
for (uint32_t i = 0; i < length; ++i) {
230255
Local<Value> key_value;
231256
if (!property_names->Get(context, i).ToLocal(&key_value)) {
257+
ThrowEventError(isolate, "Failed to get key from storageMap");
232258
return;
233259
}
234260
Local<Value> value_value;
235261
if (!storage_map_obj->Get(context, key_value).ToLocal(&value_value)) {
262+
ThrowEventError(isolate, "Failed to get value from storageMap");
236263
return;
237264
}
238265
node::Utf8Value key_utf8(isolate, key_value);

0 commit comments

Comments
 (0)