Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: attach location info to syntax errors #4013

Merged
merged 2 commits into from
Nov 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ function REPLServer(prompt,
self._domain.on('error', function(e) {
debug('domain error');
const top = replMap.get(self);
util.decorateErrorStack(e);
top.outputStream.write((e.stack || e) + '\n');
top.lineParser.reset();
top.bufferedCommand = '';
Expand Down
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,14 @@ exports._exceptionWithHostPort = function(err,
}
return ex;
};


exports.decorateErrorStack = function(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason it's not in internal/util ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I tried using internal/util, but the REPL doesn't seem to like it. It gives this error when starting node:

Error: Cannot find module 'internal/util'

if (!(isError(err) && err.stack))
return;

const arrow = internalUtil.getHiddenValue(err, 'arrowMessage');

if (arrow)
err.stack = arrow + err.stack;
};
39 changes: 39 additions & 0 deletions test/parallel/test-repl-syntax-error-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const path = require('path');
const repl = require('repl');
const util = require('util');
let found = false;

process.on('exit', () => {
assert.strictEqual(found, true);
});

// A stream to push an array into a REPL
function ArrayStream() {
this.run = function(data) {
data.forEach(line => {
this.emit('data', line + '\n');
});
};
}
util.inherits(ArrayStream, require('stream').Stream);
ArrayStream.prototype.readable = true;
ArrayStream.prototype.writable = true;
ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function(output) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use common.mustCall here to make sure that this is called.

if (/var foo bar;/.test(output))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to assert the result in process's exit, simply assert here itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.mustCall() doesn't really do the trick here. This is called multiple times. So, while I agree that common.mustCall() is better in general, I think checking on process exit is better here.

found = true;
};

const putIn = new ArrayStream();
const testMe = repl.start('', putIn);
let file = path.resolve(__dirname, '../fixtures/syntax/bad_syntax');

if (common.isWindows)
file = file.replace(/\\/g, '\\\\');

putIn.run(['.clear']);
putIn.run([`require('${file}');`]);
35 changes: 35 additions & 0 deletions test/parallel/test-util-decorate-error-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');

assert.doesNotThrow(function() {
util.decorateErrorStack();
util.decorateErrorStack(null);
util.decorateErrorStack(1);
util.decorateErrorStack(true);
});

// Verify that a stack property is not added to non-Errors
const obj = {};
util.decorateErrorStack(obj);
assert.strictEqual(obj.stack, undefined);

// Verify that the stack is decorated when possible
let err;

try {
require('../fixtures/syntax/bad_syntax');
} catch (e) {
err = e;
assert(!/var foo bar;/.test(err.stack));
util.decorateErrorStack(err);
}

assert(/var foo bar;/.test(err.stack));

// Verify that the stack is unchanged when there is no arrow message
err = new Error('foo');
const originalStack = err.stack;
util.decorateErrorStack(err);
assert.strictEqual(originalStack, err.stack);