@@ -368,6 +368,16 @@ bool IsFilePath(const std::string& path) {
368368}
369369#endif // __POSIX__
370370
371+ void ThrowUninitializedInspectorError (Environment* env) {
372+ HandleScope scope (env->isolate ());
373+
374+ const char * msg = " This Environment was initialized without a V8::Inspector" ;
375+ Local<Value> exception =
376+ v8::String::NewFromUtf8 (env->isolate (), msg).ToLocalChecked ();
377+
378+ env->isolate ()->ThrowException (exception);
379+ }
380+
371381} // namespace
372382
373383class NodeInspectorClient : public V8InspectorClient {
@@ -728,6 +738,11 @@ bool Agent::StartIoThread() {
728738 if (io_ != nullptr )
729739 return true ;
730740
741+ if (!parent_env_->should_create_inspector () && !client_) {
742+ ThrowUninitializedInspectorError (parent_env_);
743+ return false ;
744+ }
745+
731746 CHECK_NOT_NULL (client_);
732747
733748 io_ = InspectorIo::Start (client_->getThreadHandle (),
@@ -748,7 +763,13 @@ void Agent::Stop() {
748763std::unique_ptr<InspectorSession> Agent::Connect (
749764 std::unique_ptr<InspectorSessionDelegate> delegate,
750765 bool prevent_shutdown) {
766+ if (!parent_env_->should_create_inspector () && !client_) {
767+ ThrowUninitializedInspectorError (parent_env_);
768+ return std::unique_ptr<InspectorSession>{};
769+ }
770+
751771 CHECK_NOT_NULL (client_);
772+
752773 int session_id = client_->connectFrontend (std::move (delegate),
753774 prevent_shutdown);
754775 return std::unique_ptr<InspectorSession>(
@@ -758,6 +779,11 @@ std::unique_ptr<InspectorSession> Agent::Connect(
758779std::unique_ptr<InspectorSession> Agent::ConnectToMainThread (
759780 std::unique_ptr<InspectorSessionDelegate> delegate,
760781 bool prevent_shutdown) {
782+ if (!parent_env_->should_create_inspector () && !client_) {
783+ ThrowUninitializedInspectorError (parent_env_);
784+ return std::unique_ptr<InspectorSession>{};
785+ }
786+
761787 CHECK_NOT_NULL (parent_handle_);
762788 CHECK_NOT_NULL (client_);
763789 auto thread_safe_delegate =
@@ -767,6 +793,11 @@ std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
767793}
768794
769795void Agent::WaitForDisconnect () {
796+ if (!parent_env_->should_create_inspector () && !client_) {
797+ ThrowUninitializedInspectorError (parent_env_);
798+ return ;
799+ }
800+
770801 CHECK_NOT_NULL (client_);
771802 bool is_worker = parent_handle_ != nullptr ;
772803 parent_handle_.reset ();
@@ -912,6 +943,12 @@ void Agent::SetParentHandle(
912943
913944std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle (
914945 uint64_t thread_id, const std::string& url) {
946+ if (!parent_env_->should_create_inspector () && !client_) {
947+ ThrowUninitializedInspectorError (parent_env_);
948+ return std::unique_ptr<ParentInspectorHandle>{};
949+ }
950+
951+ CHECK_NOT_NULL (client_);
915952 if (!parent_handle_) {
916953 return client_->getWorkerManager ()->NewParentHandle (thread_id, url);
917954 } else {
@@ -920,11 +957,21 @@ std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
920957}
921958
922959void Agent::WaitForConnect () {
960+ if (!parent_env_->should_create_inspector () && !client_) {
961+ ThrowUninitializedInspectorError (parent_env_);
962+ return ;
963+ }
964+
923965 CHECK_NOT_NULL (client_);
924966 client_->waitForFrontend ();
925967}
926968
927969std::shared_ptr<WorkerManager> Agent::GetWorkerManager () {
970+ if (!parent_env_->should_create_inspector () && !client_) {
971+ ThrowUninitializedInspectorError (parent_env_);
972+ return std::unique_ptr<WorkerManager>{};
973+ }
974+
928975 CHECK_NOT_NULL (client_);
929976 return client_->getWorkerManager ();
930977}
0 commit comments