inspector: migrate errors from C++ to JS
Assign a code to a user-facing error. Turn other internal-only errors to checks. PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
258bcb9bff
commit
4e1f0907da
@ -30,9 +30,13 @@ class Session extends EventEmitter {
|
|||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
if (this[connectionSymbol])
|
if (this[connectionSymbol])
|
||||||
throw new ERR_INSPECTOR_ALREADY_CONNECTED();
|
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
|
||||||
this[connectionSymbol] =
|
const connection =
|
||||||
new Connection((message) => this[onMessageSymbol](message));
|
new Connection((message) => this[onMessageSymbol](message));
|
||||||
|
if (connection.sessionAttached) {
|
||||||
|
throw new ERR_INSPECTOR_ALREADY_CONNECTED('Another inspector session');
|
||||||
|
}
|
||||||
|
this[connectionSymbol] = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
[onMessageSymbol](message) {
|
[onMessageSymbol](message) {
|
||||||
|
@ -739,8 +739,7 @@ E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
|
|||||||
E('ERR_HTTP_TRAILER_INVALID',
|
E('ERR_HTTP_TRAILER_INVALID',
|
||||||
'Trailers are invalid with this transfer encoding', Error);
|
'Trailers are invalid with this transfer encoding', Error);
|
||||||
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range', RangeError);
|
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range', RangeError);
|
||||||
E('ERR_INSPECTOR_ALREADY_CONNECTED',
|
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
|
||||||
'The inspector is already connected', Error);
|
|
||||||
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
|
E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
|
||||||
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
|
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
|
||||||
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
|
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
|
||||||
|
@ -9,6 +9,7 @@ namespace node {
|
|||||||
namespace inspector {
|
namespace inspector {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
using v8::Boolean;
|
||||||
using v8::Context;
|
using v8::Context;
|
||||||
using v8::Function;
|
using v8::Function;
|
||||||
using v8::FunctionCallbackInfo;
|
using v8::FunctionCallbackInfo;
|
||||||
@ -78,7 +79,11 @@ class JSBindingsConnection : public AsyncWrap {
|
|||||||
|
|
||||||
Agent* inspector = env->inspector_agent();
|
Agent* inspector = env->inspector_agent();
|
||||||
if (inspector->delegate() != nullptr) {
|
if (inspector->delegate() != nullptr) {
|
||||||
env->ThrowTypeError("Session is already attached");
|
// This signals JS code that it has to throw an error.
|
||||||
|
Local<String> session_attached =
|
||||||
|
FIXED_ONE_BYTE_STRING(env->isolate(), "sessionAttached");
|
||||||
|
wrap->Set(env->context(), session_attached,
|
||||||
|
Boolean::New(env->isolate(), true)).ToChecked();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
inspector->Connect(&delegate_);
|
inspector->Connect(&delegate_);
|
||||||
@ -95,10 +100,7 @@ class JSBindingsConnection : public AsyncWrap {
|
|||||||
|
|
||||||
static void New(const FunctionCallbackInfo<Value>& info) {
|
static void New(const FunctionCallbackInfo<Value>& info) {
|
||||||
Environment* env = Environment::GetCurrent(info);
|
Environment* env = Environment::GetCurrent(info);
|
||||||
if (!info[0]->IsFunction()) {
|
CHECK(info[0]->IsFunction());
|
||||||
env->ThrowTypeError("Message callback is required");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Local<Function> callback = info[0].As<Function>();
|
Local<Function> callback = info[0].As<Function>();
|
||||||
new JSBindingsConnection(env, info.This(), callback);
|
new JSBindingsConnection(env, info.This(), callback);
|
||||||
}
|
}
|
||||||
@ -121,10 +123,7 @@ class JSBindingsConnection : public AsyncWrap {
|
|||||||
Environment* env = Environment::GetCurrent(info);
|
Environment* env = Environment::GetCurrent(info);
|
||||||
JSBindingsConnection* session;
|
JSBindingsConnection* session;
|
||||||
ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
|
ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
|
||||||
if (!info[0]->IsString()) {
|
CHECK(info[0]->IsString());
|
||||||
env->ThrowTypeError("Inspector message must be a string");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
session->CheckIsCurrent();
|
session->CheckIsCurrent();
|
||||||
Agent* inspector = env->inspector_agent();
|
Agent* inspector = env->inspector_agent();
|
||||||
@ -143,10 +142,9 @@ void AddCommandLineAPI(const FunctionCallbackInfo<Value>& info) {
|
|||||||
auto env = Environment::GetCurrent(info);
|
auto env = Environment::GetCurrent(info);
|
||||||
Local<Context> context = env->context();
|
Local<Context> context = env->context();
|
||||||
|
|
||||||
if (info.Length() != 2 || !info[0]->IsString()) {
|
// inspector.addCommandLineAPI takes 2 arguments: a string and a value.
|
||||||
return env->ThrowTypeError("inspector.addCommandLineAPI takes "
|
CHECK_EQ(info.Length(), 2);
|
||||||
"exactly 2 arguments: a string and a value.");
|
CHECK(info[0]->IsString());
|
||||||
}
|
|
||||||
|
|
||||||
Local<Object> console_api = env->inspector_console_api_object();
|
Local<Object> console_api = env->inspector_console_api_object();
|
||||||
console_api->Set(context, info[0], info[1]).FromJust();
|
console_api->Set(context, info[0], info[1]).FromJust();
|
||||||
|
@ -51,7 +51,17 @@ common.expectsError(
|
|||||||
{
|
{
|
||||||
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
|
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
|
||||||
type: Error,
|
type: Error,
|
||||||
message: 'The inspector is already connected'
|
message: 'The inspector session is already connected'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const session2 = new Session();
|
||||||
|
common.expectsError(
|
||||||
|
() => session2.connect(),
|
||||||
|
{
|
||||||
|
code: 'ERR_INSPECTOR_ALREADY_CONNECTED',
|
||||||
|
type: Error,
|
||||||
|
message: 'Another inspector session is already connected'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user