Skip to content

Commit 4e1f090

Browse files
committed
inspector: migrate errors from C++ to JS
Assign a code to a user-facing error. Turn other internal-only errors to checks. PR-URL: #19387 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 258bcb9 commit 4e1f090

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

lib/inspector.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ class Session extends EventEmitter {
3030

3131
connect() {
3232
if (this[connectionSymbol])
33-
throw new ERR_INSPECTOR_ALREADY_CONNECTED();
34-
this[connectionSymbol] =
35-
new Connection((message) => this[onMessageSymbol](message));
33+
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
34+
const connection =
35+
new Connection((message) => this[onMessageSymbol](message));
36+
if (connection.sessionAttached) {
37+
throw new ERR_INSPECTOR_ALREADY_CONNECTED('Another inspector session');
38+
}
39+
this[connectionSymbol] = connection;
3640
}
3741

3842
[onMessageSymbol](message) {

lib/internal/errors.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,7 @@ E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
739739
E('ERR_HTTP_TRAILER_INVALID',
740740
'Trailers are invalid with this transfer encoding', Error);
741741
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range', RangeError);
742-
E('ERR_INSPECTOR_ALREADY_CONNECTED',
743-
'The inspector is already connected', Error);
742+
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
744743
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
745744
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
746745
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);

src/inspector_js_api.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace node {
99
namespace inspector {
1010
namespace {
1111

12+
using v8::Boolean;
1213
using v8::Context;
1314
using v8::Function;
1415
using v8::FunctionCallbackInfo;
@@ -78,7 +79,11 @@ class JSBindingsConnection : public AsyncWrap {
7879

7980
Agent* inspector = env->inspector_agent();
8081
if (inspector->delegate() != nullptr) {
81-
env->ThrowTypeError("Session is already attached");
82+
// This signals JS code that it has to throw an error.
83+
Local<String> session_attached =
84+
FIXED_ONE_BYTE_STRING(env->isolate(), "sessionAttached");
85+
wrap->Set(env->context(), session_attached,
86+
Boolean::New(env->isolate(), true)).ToChecked();
8287
return;
8388
}
8489
inspector->Connect(&delegate_);
@@ -95,10 +100,7 @@ class JSBindingsConnection : public AsyncWrap {
95100

96101
static void New(const FunctionCallbackInfo<Value>& info) {
97102
Environment* env = Environment::GetCurrent(info);
98-
if (!info[0]->IsFunction()) {
99-
env->ThrowTypeError("Message callback is required");
100-
return;
101-
}
103+
CHECK(info[0]->IsFunction());
102104
Local<Function> callback = info[0].As<Function>();
103105
new JSBindingsConnection(env, info.This(), callback);
104106
}
@@ -121,10 +123,7 @@ class JSBindingsConnection : public AsyncWrap {
121123
Environment* env = Environment::GetCurrent(info);
122124
JSBindingsConnection* session;
123125
ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
124-
if (!info[0]->IsString()) {
125-
env->ThrowTypeError("Inspector message must be a string");
126-
return;
127-
}
126+
CHECK(info[0]->IsString());
128127

129128
session->CheckIsCurrent();
130129
Agent* inspector = env->inspector_agent();
@@ -143,10 +142,9 @@ void AddCommandLineAPI(const FunctionCallbackInfo<Value>& info) {
143142
auto env = Environment::GetCurrent(info);
144143
Local<Context> context = env->context();
145144

146-
if (info.Length() != 2 || !info[0]->IsString()) {
147-
return env->ThrowTypeError("inspector.addCommandLineAPI takes "
148-
"exactly 2 arguments: a string and a value.");
149-
}
145+
// inspector.addCommandLineAPI takes 2 arguments: a string and a value.
146+
CHECK_EQ(info.Length(), 2);
147+
CHECK(info[0]->IsString());
150148

151149
Local<Object> console_api = env->inspector_console_api_object();
152150
console_api->Set(context, info[0], info[1]).FromJust();

test/sequential/test-inspector-module.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ common.expectsError(
5151
{
5252
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
5353
type: Error,
54-
message: 'The inspector is already connected'
54+
message: 'The inspector session is already connected'
55+
}
56+
);
57+
58+
const session2 = new Session();
59+
common.expectsError(
60+
() => session2.connect(),
61+
{
62+
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
63+
type: Error,
64+
message: 'Another inspector session is already connected'
5565
}
5666
);
5767

0 commit comments

Comments
 (0)