Skip to content

Commit 063066c

Browse files
committed
handle null error
1 parent 5639261 commit 063066c

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

lib/wrap.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,81 @@
1-
var path = require('path');
2-
var childProcess = require('child_process');
3-
var fork = childProcess.fork;
4-
var resolve = require('resolve').sync;
5-
var hook = require('./hook');
6-
var ipc = require('./ipc');
7-
var resolveMain = require('./resolveMain');
1+
var path = require('path')
2+
var childProcess = require('child_process')
3+
var fork = childProcess.fork
4+
var resolve = require('resolve').sync
5+
var hook = require('./hook')
6+
var ipc = require('./ipc')
7+
var resolveMain = require('./resolveMain')
88

99
// Remove wrap.js from the argv array
10-
process.argv.splice(1, 1);
10+
process.argv.splice(1, 1)
1111

1212
// Resolve the location of the main script relative to cwd
13-
var main = resolveMain(process.argv[1]);
13+
var main = resolveMain(process.argv[1])
1414

15-
var cfg = require('./cfg')(main, {});
15+
var cfg = require('./cfg')(main, {})
1616

1717
// Set NODE_ENV to 'development' unless already set
18-
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development';
18+
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development'
1919

2020
if (process.env.NODE_DEV_PRELOAD) {
21-
require(process.env.NODE_DEV_PRELOAD);
21+
require(process.env.NODE_DEV_PRELOAD)
2222
}
2323

2424
// Listen SIGTERM and exit unless there is another listener
2525
process.on('SIGTERM', function () {
26-
if (process.listeners('SIGTERM').length === 1) process.exit(0);
27-
});
26+
if (process.listeners('SIGTERM').length === 1) process.exit(0)
27+
})
2828

2929
if (cfg.fork) {
3030
// Overwrite child_process.fork() so that we can hook into forked processes
3131
// too. We also need to relay messages about required files to the parent.
3232
childProcess.fork = function (modulePath, args, options) {
33-
var child = fork(__filename, [modulePath].concat(args), options);
34-
ipc.relay(child);
35-
return child;
36-
};
33+
var child = fork(__filename, [modulePath].concat(args), options)
34+
ipc.relay(child)
35+
return child
36+
}
3737
}
3838

3939
// Error handler that displays a notification and logs the stack to stderr:
40-
var caught = false;
40+
var caught = false
4141
process.on('uncaughtException', function (err) {
42-
// Handle exepection only once
43-
if (caught) return;
44-
caught = true;
42+
// NB: err can be null
43+
// Handle exception only once
44+
if (caught) return
45+
caught = true
4546
// If there's a custom uncaughtException handler expect it to terminate
4647
// the process.
47-
var hasCustomHandler = process.listeners('uncaughtException').length > 1;
48-
var isTsError = err.message && /TypeScript/.test(err.message)
49-
if (!hasCustomHandler && !isTsError) {
50-
console.error(err.stack || err);
51-
}
48+
var hasCustomHandler = process.listeners('uncaughtException').length > 1
49+
var isTsError = err && err.message && /TypeScript/.test(err.message)
50+
if (!hasCustomHandler && !isTsError) {
51+
console.error((err && err.stack) || err)
52+
}
5253
ipc.send({
53-
error: isTsError ? '' : err.name || 'Error',
54-
message: err.message,
55-
willTerminate: hasCustomHandler
56-
});
57-
});
54+
error: isTsError ? '' : err && err.name || 'Error',
55+
message: err ? err.message : '',
56+
willTerminate: hasCustomHandler,
57+
})
58+
})
5859

5960
// Hook into require() and notify the parent process about required files
6061
hook(cfg, module, function (file) {
61-
ipc.send({ required: file });
62-
});
62+
ipc.send({ required: file })
63+
})
6364

6465
// Check if a module is registered for this extension
65-
var ext = path.extname(main).slice(1);
66-
var mod = cfg.extensions[ext];
66+
var ext = path.extname(main).slice(1)
67+
var mod = cfg.extensions[ext]
6768

6869
// Support extensions where 'require' returns a function that accepts options
6970
if (typeof mod == 'object' && mod.name) {
70-
var fn = require(resolve(mod.name, { basedir: path.dirname(main) }));
71+
var fn = require(resolve(mod.name, { basedir: path.dirname(main) }))
7172
if (typeof fn == 'function' && mod.options) {
7273
// require returned a function, call it with options
73-
fn(mod.options);
74+
fn(mod.options)
7475
}
7576
} else if (typeof mod == 'string') {
76-
require(resolve(mod, { basedir: path.dirname(main) }));
77+
require(resolve(mod, { basedir: path.dirname(main) }))
7778
}
7879

7980
// Execute the wrapped script
80-
require(main);
81+
require(main)

0 commit comments

Comments
 (0)