chore: better logging if Node initialization fails (#45286)

feat: better logging if Node initialization fails
This commit is contained in:
Charles Kerr 2025-01-23 08:55:28 -06:00 committed by GitHub
parent 863faea542
commit db7ef90159
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 74 additions and 21 deletions

View file

@ -5,7 +5,12 @@
#include "shell/common/node_util.h"
#include "base/compiler_specific.h"
#include "base/containers/to_value_list.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "gin/converter.h"
#include "gin/dictionary.h"
#include "shell/browser/javascript_environment.h"
@ -76,4 +81,51 @@ base::span<uint8_t> as_byte_span(v8::Local<v8::Value> node_buffer) {
return UNSAFE_BUFFERS(base::span{data, size});
}
node::Environment* CreateEnvironment(v8::Isolate* isolate,
node::IsolateData* isolate_data,
v8::Local<v8::Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
node::EnvironmentFlags::Flags env_flags,
std::string_view process_type) {
v8::TryCatch try_catch{isolate};
node::Environment* env = node::CreateEnvironment(isolate_data, context, args,
exec_args, env_flags);
if (auto message = try_catch.Message(); !message.IsEmpty()) {
base::Value::Dict dict;
if (std::string str; gin::ConvertFromV8(isolate, message->Get(), &str))
dict.Set("message", std::move(str));
if (std::string str; gin::ConvertFromV8(
isolate, message->GetScriptOrigin().ResourceName(), &str)) {
const auto line_num = message->GetLineNumber(context).FromJust();
const auto line_str = base::NumberToString(line_num);
dict.Set("location", base::StrCat({", at ", str, ":", line_str}));
}
if (std::string str; gin::ConvertFromV8(
isolate, message->GetSourceLine(context).ToLocalChecked(), &str))
dict.Set("source_line", std::move(str));
if (!std::empty(process_type))
dict.Set("process_type", process_type);
if (auto list = base::ToValueList(args); !std::empty(list))
dict.Set("args", std::move(list));
if (auto list = base::ToValueList(exec_args); !std::empty(list))
dict.Set("exec_args", std::move(list));
std::string errstr = "Failed to initialize Node.js.";
if (std::optional<std::string> jsonstr = base::WriteJsonWithOptions(
dict, base::JsonOptions::OPTIONS_PRETTY_PRINT))
errstr += base::StrCat({" ", *jsonstr});
LOG(ERROR) << errstr;
}
return env;
}
} // namespace electron::util