Skip to content

Commit

Permalink
fix: handle electron script errors better (electron#25331) (electron#…
Browse files Browse the repository at this point in the history
…25454)

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and adill committed Sep 24, 2020
1 parent 27ec42a commit 00acc89
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
15 changes: 14 additions & 1 deletion build/webpack/run-compiler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')

Expand All @@ -9,14 +10,26 @@ config.output = {
filename: path.basename(outPath)
}

webpack(config, (err, stats) => {
const { wrapInitWithTryCatch, ...webpackConfig } = config;

webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err)
process.exit(1)
} else if (stats.hasErrors()) {
console.error(stats.toString('normal'))
process.exit(1)
} else {
let contents = fs.readFileSync(outPath, 'utf8');
if (wrapInitWithTryCatch) {
contents = `try {
${contents}
} catch (err) {
console.error('Electron ${webpackConfig.output.filename} script failed to run');
console.error(err);
}`;
}
fs.writeFileSync(outPath, contents)
process.exit(0)
}
})
4 changes: 3 additions & 1 deletion build/webpack/webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = ({
alwaysHasNode,
loadElectronFromAlternateTarget,
targetDeletesNodeGlobals,
target
target,
wrapInitWithTryCatch
}) => {
let entry = path.resolve(electronRoot, 'lib', target, 'init.ts')
if (!fs.existsSync(entry)) {
Expand All @@ -39,6 +40,7 @@ module.exports = ({
output: {
filename: `${target}.bundle.js`
},
wrapInitWithTryCatch,
resolve: {
alias: {
'@electron/internal': path.resolve(electronRoot, 'lib'),
Expand Down
3 changes: 2 additions & 1 deletion build/webpack/webpack.config.isolated_renderer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = require('./webpack.config.base')({
target: 'isolated_renderer',
alwaysHasNode: false
alwaysHasNode: false,
wrapInitWithTryCatch: true
})
3 changes: 2 additions & 1 deletion build/webpack/webpack.config.renderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = require('./webpack.config.base')({
target: 'renderer',
alwaysHasNode: true,
targetDeletesNodeGlobals: true
targetDeletesNodeGlobals: true,
wrapInitWithTryCatch: true
})
3 changes: 2 additions & 1 deletion build/webpack/webpack.config.sandboxed_renderer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = require('./webpack.config.base')({
target: 'sandboxed_renderer',
alwaysHasNode: false
alwaysHasNode: false,
wrapInitWithTryCatch: true
})
3 changes: 2 additions & 1 deletion build/webpack/webpack.config.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = require('./webpack.config.base')({
target: 'worker',
loadElectronFromAlternateTarget: 'renderer',
alwaysHasNode: true,
targetDeletesNodeGlobals: true
targetDeletesNodeGlobals: true,
wrapInitWithTryCatch: true
})
23 changes: 19 additions & 4 deletions shell/common/node_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,25 @@ node::Environment* NodeBindings::CreateEnvironment(
args.insert(args.begin() + 1, init_script);

std::unique_ptr<const char*[]> c_argv = StringVectorToArgArray(args);
node::Environment* env = node::CreateEnvironment(
node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform),
context, args.size(), c_argv.get(), 0, nullptr, bootstrap_env);
DCHECK(env);
node::Environment* env;
if (browser_env_ != BrowserEnvironment::BROWSER) {
v8::TryCatch try_catch(context->GetIsolate());
env = node::CreateEnvironment(
node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform),
context, args.size(), c_argv.get(), 0, nullptr);
DCHECK(env);
// This will only be caught when something has gone terrible wrong as all
// electron scripts are wrapped in a try {} catch {} in run-compiler.js
if (try_catch.HasCaught()) {
LOG(ERROR) << "Failed to initialize node environment in process: "
<< process_type;
}
} else {
env = node::CreateEnvironment(
node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform),
context, args.size(), c_argv.get(), 0, nullptr);
DCHECK(env);
}

// Clean up the global _noBrowserGlobals that we unironically injected into
// the global scope
Expand Down
13 changes: 10 additions & 3 deletions shell/common/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
// found in the LICENSE file.

#include "shell/common/node_util.h"
#include "base/logging.h"
#include "shell/common/node_includes.h"
#include "third_party/electron_node/src/node_native_module_env.h"

namespace electron {

namespace util {

v8::MaybeLocal<v8::Value> CompileAndCall(
v8::Local<v8::Context> context,
const char* id,
std::vector<v8::Local<v8::String>>* parameters,
std::vector<v8::Local<v8::Value>>* arguments,
node::Environment* optional_env) {
v8::Isolate* isolate = context->GetIsolate();
v8::TryCatch try_catch(isolate);
v8::MaybeLocal<v8::Function> compiled =
node::native_module::NativeModuleEnv::LookupAndCompile(
context, id, parameters, optional_env);
Expand All @@ -26,8 +26,15 @@ v8::MaybeLocal<v8::Value> CompileAndCall(
v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
return fn->Call(context, v8::Null(isolate), arguments->size(),
arguments->data());
v8::MaybeLocal<v8::Value> ret = fn->Call(
context, v8::Null(isolate), arguments->size(), arguments->data());
// This will only be caught when something has gone terrible wrong as all
// electron scripts are wrapped in a try {} catch {} in run-compiler.js
if (try_catch.HasCaught()) {
LOG(ERROR) << "Failed to CompileAndCall electron script: " << id;
}
return ret;
}

} // namespace util

} // namespace electron

0 comments on commit 00acc89

Please sign in to comment.