inspector: return errors when CDP protocol event emission fails · nodejs/node@3dc3fb6
@@ -17,18 +17,26 @@ using v8::Local;
1717using v8::Object;
1818using v8::Value;
191920+static void ThrowEventError(v8::Isolate* isolate, const std::string& message) {
21+ isolate->ThrowException(v8::Exception::TypeError(
22+v8::String::NewFromUtf8(isolate, message.c_str()).ToLocalChecked()));
23+}
24+2025std::unique_ptr<protocol::DOMStorage::StorageId> createStorageIdFromObject(
2126 Local<Context> context, Local<Object> storage_id_obj) {
2227 protocol::String security_origin;
28+ Isolate* isolate = Isolate::GetCurrent();
2329if (!ObjectGetProtocolString(context, storage_id_obj, "securityOrigin")
2430 .To(&security_origin)) {
31+ThrowEventError(isolate, "Missing securityOrigin in storageId");
2532return {};
2633 }
2734bool is_local_storage =
2835ObjectGetBool(context, storage_id_obj, "isLocalStorage").FromMaybe(false);
2936 protocol::String storageKey;
3037if (!ObjectGetProtocolString(context, storage_id_obj, "storageKey")
3138 .To(&storageKey)) {
39+ThrowEventError(isolate, "Missing storageKey in storageId");
3240return {};
3341 }
3442@@ -135,8 +143,10 @@ protocol::DispatchResponse DOMStorageAgent::clear(
135143136144void DOMStorageAgent::domStorageItemAdded(Local<Context> context,
137145 Local<Object> params) {
146+ Isolate* isolate = env_->isolate();
138147 Local<Object> storage_id_obj;
139148if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
149+ThrowEventError(isolate, "Missing storageId in event");
140150return;
141151 }
142152@@ -148,19 +158,23 @@ void DOMStorageAgent::domStorageItemAdded(Local<Context> context,
148158149159 protocol::String key;
150160if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
161+ThrowEventError(isolate, "Missing key in event");
151162return;
152163 }
153164 protocol::String new_value;
154165if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) {
166+ThrowEventError(isolate, "Missing newValue in event");
155167return;
156168 }
157169 frontend_->domStorageItemAdded(std::move(storage_id), key, new_value);
158170}
159171160172void DOMStorageAgent::domStorageItemRemoved(Local<Context> context,
161173 Local<Object> params) {
174+ Isolate* isolate = env_->isolate();
162175 Local<Object> storage_id_obj;
163176if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
177+ThrowEventError(isolate, "Missing storageId in event");
164178return;
165179 }
166180 std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
@@ -172,15 +186,18 @@ void DOMStorageAgent::domStorageItemRemoved(Local<Context> context,
172186173187 protocol::String key;
174188if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
189+ThrowEventError(isolate, "Missing key in event");
175190return;
176191 }
177192 frontend_->domStorageItemRemoved(std::move(storage_id), key);
178193}
179194180195void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
181196 Local<Object> params) {
197+ Isolate* isolate = env_->isolate();
182198 Local<Object> storage_id_obj;
183199if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
200+ThrowEventError(isolate, "Missing storageId in event");
184201return;
185202 }
186203@@ -193,14 +210,17 @@ void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
193210194211 protocol::String key;
195212if (!ObjectGetProtocolString(context, params, "key").To(&key)) {
213+ThrowEventError(isolate, "Missing key in event");
196214return;
197215 }
198216 protocol::String old_value;
199217if (!ObjectGetProtocolString(context, params, "oldValue").To(&old_value)) {
218+ThrowEventError(isolate, "Missing oldValue in event");
200219return;
201220 }
202221 protocol::String new_value;
203222if (!ObjectGetProtocolString(context, params, "newValue").To(&new_value)) {
223+ThrowEventError(isolate, "Missing newValue in event");
204224return;
205225 }
206226 frontend_->domStorageItemUpdated(
@@ -209,8 +229,10 @@ void DOMStorageAgent::domStorageItemUpdated(Local<Context> context,
209229210230void DOMStorageAgent::domStorageItemsCleared(Local<Context> context,
211231 Local<Object> params) {
232+ Isolate* isolate = env_->isolate();
212233 Local<Object> storage_id_obj;
213234if (!ObjectGetObject(context, params, "storageId").ToLocal(&storage_id_obj)) {
235+ThrowEventError(isolate, "Missing storageId in event");
214236return;
215237 }
216238 std::unique_ptr<protocol::DOMStorage::StorageId> storage_id =
@@ -228,27 +250,32 @@ void DOMStorageAgent::registerStorage(Local<Context> context,
228250 HandleScope handle_scope(isolate);
229251bool is_local_storage;
230252if (!ObjectGetBool(context, params, "isLocalStorage").To(&is_local_storage)) {
253+ThrowEventError(isolate, "Missing isLocalStorage in event");
231254return;
232255 }
233256 Local<Object> storage_map_obj;
234257if (!ObjectGetObject(context, params, "storageMap")
235258 .ToLocal(&storage_map_obj)) {
259+ThrowEventError(isolate, "Missing storageMap in event");
236260return;
237261 }
238262 StorageMap& storage_map =
239263 is_local_storage ? local_storage_map_ : session_storage_map_;
240264 Local<Array> property_names;
241265if (!storage_map_obj->GetOwnPropertyNames(context).ToLocal(&property_names)) {
266+ThrowEventError(isolate, "Failed to get property names from storageMap");
242267return;
243268 }
244269uint32_t length = property_names->Length();
245270for (uint32_t i = 0; i < length; ++i) {
246271 Local<Value> key_value;
247272if (!property_names->Get(context, i).ToLocal(&key_value)) {
273+ThrowEventError(isolate, "Failed to get key from storageMap");
248274return;
249275 }
250276 Local<Value> value_value;
251277if (!storage_map_obj->Get(context, key_value).ToLocal(&value_value)) {
278+ThrowEventError(isolate, "Failed to get value from storageMap");
252279return;
253280 }
254281 node::TwoByteValue key_utf16(isolate, key_value);