@@ -91,10 +91,11 @@ json NetworkCacheReply(long long id, const std::string& method, const json& mess
9191 return {{" id" , id}, {" result" , json::object ()}};
9292}
9393
94- // The runtime's reply to `message` when it is a Network.setCacheDisabled or
95- // Network.clearBrowserCache command for the main session.
96- std::optional<std::string> MainSessionNetworkCacheReply (const std::string& message) {
97- if (message.find (" Network." ) == std::string::npos) {
94+ // `message` parsed, when it is a Network.setCacheDisabled or
95+ // Network.clearBrowserCache command with an id for the main session.
96+ std::optional<json> ParseMainSessionNetworkCacheCommand (const std::string& message) {
97+ if (message.find (" Network.setCacheDisabled" ) == std::string::npos &&
98+ message.find (" Network.clearBrowserCache" ) == std::string::npos) {
9899 return std::nullopt ;
99100 }
100101 auto parsed = json::parse (message, nullptr , false );
@@ -105,8 +106,24 @@ std::optional<std::string> MainSessionNetworkCacheReply(const std::string& messa
105106 !parsed[" sessionId" ].get <std::string>().empty ())) {
106107 return std::nullopt ;
107108 }
108- return JsonDump (NetworkCacheReply (parsed[" id" ].get <long long >(),
109- parsed[" method" ].get <std::string>(), parsed));
109+ return parsed;
110+ }
111+
112+ // The runtime's reply to `message` when it is a main-session
113+ // Network.setCacheDisabled or Network.clearBrowserCache command.
114+ std::optional<std::string> MainSessionNetworkCacheReply (const std::string& message) {
115+ std::optional<json> parsed = ParseMainSessionNetworkCacheCommand (message);
116+ if (!parsed) {
117+ return std::nullopt ;
118+ }
119+ return JsonDump (NetworkCacheReply ((*parsed)[" id" ].get <long long >(),
120+ (*parsed)[" method" ].get <std::string>(), *parsed));
121+ }
122+
123+ json SessionNotFoundReply (long long id, const std::string& sessionId) {
124+ return {{" id" , id},
125+ {" sessionId" , sessionId},
126+ {" error" , {{" code" , -32001 }, {" message" , " Session not found" }}}};
110127}
111128
112129// Target.attachedToTarget event announcing a worker as a child target.
@@ -474,7 +491,9 @@ void JsV8InspectorClient::dispatchMessage(const std::string& message) {
474491 Local<v8::Function> domainMethodFunc = GetDebuggerFunctionFromObject (context, argObject, domainDebugger);
475492
476493 Local<Value> result;
477- success = this ->CallDomainHandlerFunction (context, domainMethodFunc, argObject, domainDebugger, result);
494+ std::optional<std::string> thrownMessage;
495+ success = this ->CallDomainHandlerFunction (context, domainMethodFunc, argObject,
496+ domainDebugger, result, thrownMessage);
478497
479498 if (success) {
480499 auto requestId = arg.As <Object>()->Get (context, tns::ArgConverter::ConvertToV8String (isolate_, " id" )).ToLocalChecked ();
@@ -490,6 +509,17 @@ void JsV8InspectorClient::dispatchMessage(const std::string& message) {
490509 }
491510 return ;
492511 }
512+
513+ // The handler owns these commands, so the runtime's own reply in
514+ // doDispatchMessage must not answer them as well.
515+ if (thrownMessage) {
516+ if (std::optional<json> command = ParseMainSessionNetworkCacheCommand (message)) {
517+ json error = {{" id" , (*command)[" id" ]},
518+ {" error" , {{" code" , -32000 }, {" message" , *thrownMessage}}}};
519+ this ->SendRawToFrontend (JsonDump (error));
520+ return ;
521+ }
522+ }
493523 }
494524
495525
@@ -735,6 +765,20 @@ bool JsV8InspectorClient::handleMessageOnSocketThread(const std::string& message
735765 // Network domain handler, if one is registered, for the response. Workers
736766 // have no JS domain handlers, so a worker session is answered here.
737767 if (IsNetworkCacheMethod (method)) {
768+ if (!sessionId.empty ()) {
769+ bool knownSession;
770+ {
771+ std::lock_guard<std::mutex> lock (workerTargetsMutex_);
772+ knownSession = workerTargets_.count (sessionId) != 0 ;
773+ }
774+ if (!knownSession) {
775+ if (msgId >= 0 ) {
776+ json error = SessionNotFoundReply (msgId, sessionId);
777+ response = JsonDump (error);
778+ }
779+ return true ;
780+ }
781+ }
738782 if (method == " Network.clearBrowserCache" ) {
739783 ClearModuleHttpCacheFromInspector ();
740784 } else if (std::optional<bool > cacheDisabled = SetCacheDisabledParam (parsed)) {
@@ -825,10 +869,7 @@ void JsV8InspectorClient::RouteToWorker(const std::string& sessionId, const std:
825869 // The worker died (or never existed): answer commands so the
826870 // frontend does not wait forever.
827871 if (msgId >= 0 ) {
828- json error = {{" id" , msgId},
829- {" sessionId" , sessionId},
830- {" error" , {{" code" , -32001 }, {" message" , " Session not found" }}}};
831- errorPayload = JsonDump (error);
872+ errorPayload = JsonDump (SessionNotFoundReply (msgId, sessionId));
832873 }
833874 } else {
834875 // The lock keeps the client alive against a concurrent
@@ -1372,7 +1413,7 @@ void JsV8InspectorClient::InspectorIsConnectedGetterCallback(v8::Local<v8::Strin
13721413std::atomic<JsV8InspectorClient*> JsV8InspectorClient::instance{nullptr };
13731414
13741415
1375- bool JsV8InspectorClient::CallDomainHandlerFunction (Local<Context> context, Local<Function> domainMethodFunc, const Local<Object>& arg, Local<Object>& domainDebugger, Local<Value>& result) {
1416+ bool JsV8InspectorClient::CallDomainHandlerFunction (Local<Context> context, Local<Function> domainMethodFunc, const Local<Object>& arg, Local<Object>& domainDebugger, Local<Value>& result, std::optional<std::string>& thrownMessage ) {
13761417 if (domainMethodFunc.IsEmpty () || !domainMethodFunc->IsFunction ()) {
13771418 return false ;
13781419 }
@@ -1405,6 +1446,24 @@ bool JsV8InspectorClient::CallDomainHandlerFunction(Local<Context> context, Loca
14051446 // log any other errors - they are caught, but still make them visible to the user.
14061447 // tns::LogError(isolate, tc);
14071448
1449+ // The error's message, as Chromium reports a failed command; the whole
1450+ // text for anything else that was thrown.
1451+ if (!tc.HasTerminated ()) {
1452+ Local<Value> exception = tc.Exception ();
1453+ TryCatch messageTc (isolate);
1454+ Local<Value> message;
1455+ if (exception->IsNativeError () &&
1456+ exception.As <Object>()->Get (context, tns::ArgConverter::ConvertToV8String (isolate, " message" )).ToLocal (&message) &&
1457+ message->IsString ()) {
1458+ thrownMessage = tns::ArgConverter::ToString (isolate, message);
1459+ } else if (exception->ToString (context).ToLocal (&message)) {
1460+ thrownMessage = tns::ArgConverter::ToString (isolate, message);
1461+ }
1462+ if (!thrownMessage) {
1463+ thrownMessage = std::string ();
1464+ }
1465+ }
1466+
14081467 return false ;
14091468 }
14101469
0 commit comments