support null, undefined and errors in extension output (fixes #9)

This commit is contained in:
Benjamin Pasero 2015-11-19 09:11:22 +01:00
parent 57a1b92b1b
commit 3605e5dba1
2 changed files with 39 additions and 4 deletions

31
src/bootstrap.js vendored
View file

@ -10,11 +10,36 @@ if (!!process.send && process.env.PIPE_LOGGING === 'true') {
var MAX_LENGTH = 100000;
// Prevent circular stringify
function safeStringify(obj) {
function safeStringify(args) {
var seen = [];
var res;
// Massage some arguments with special treatment
if (args.length) {
for (var i = 0; i < args.length; i++) {
// Any argument of type 'undefined' needs to be specially treated because
// JSON.stringify will simply ignore those. We replace them with the string
// 'undefined' which is not 100% right, but good enough to be logged to console
if (typeof args[i] === 'undefined') {
args[i] = 'undefined';
}
// Any argument that is an Error will be changed to be just the error stack/message
// itself because currently cannot serialize the error over entirely.
else if (args[i] instanceof Error) {
var errorObj = args[i];
if (errorObj.stack) {
args[i] = errorObj.stack;
} else {
args[i] = errorObj.toString();
}
}
}
}
try {
res = JSON.stringify(obj, function (key, value) {
res = JSON.stringify(args, function (key, value) {
// Objects get special treatment to prevent circles
if (value && Object.prototype.toString.call(value) === '[object Object]') {
@ -46,7 +71,7 @@ if (!!process.send && process.env.PIPE_LOGGING === 'true') {
console.log = function () { /* ignore */ };
console.warn = function () { /* ignore */ };
}
console.error = function () { process.send({ type: '__$console', severity: 'error', arguments: safeStringify(arguments) }); };
// Let stdout, stderr and stdin be no-op streams. This prevents an issue where we would get an EBADF

View file

@ -52,8 +52,18 @@ export class ExtensionOutputHandler implements IWorkbenchContribution {
for (let i = 0; i < args.length; i++) {
let a = args[i];
// Undefined gets printed as 'undefined'
if (typeof a === 'undefined') {
simpleVals.push('undefined');
}
// Null gets printed as 'null'
else if (a === null) {
simpleVals.push('null');
}
// Objects & Arrays are special because we want to inspect them in the REPL
if (types.isObject(a) || Array.isArray(a)) {
else if (types.isObject(a) || Array.isArray(a)) {
// Flush any existing simple values logged
if (simpleVals.length) {