repl: handle async error in repl #4491

Closed
wants to merge 1 commit into
from
View
@@ -47,6 +47,7 @@ var vm = require('vm');
var path = require('path');
var fs = require('fs');
var rl = require('readline');
+var domain = require('domain');
var Console = require('console').Console;
var EventEmitter = require('events').EventEmitter;
@@ -199,6 +200,13 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
self.displayPrompt();
});
+ var replDomain = domain.create();
+ replDomain.on('error', function(e) {
+ self.outputStream.write((e.stack || e) + '\n');
+ self.bufferedCommand = '';
+ self.displayPrompt();
AlexeyKupershtokh
AlexeyKupershtokh Mar 15, 2013

wouldn't displayPrompt(true) which preserves cursor position be better?

isaacs
isaacs Mar 15, 2013

Yes, that would be good. Also, it should write a \n before the error stack, so that it isn't to the right of the typed line.

Patch welcome.

TooTallNate
TooTallNate Mar 16, 2013

@AlexeyKupershtokh Actually I don't think displayPrompt(true) would matter in this case. The bufferedCommand already gets reset to ''' so the cursor ends up in the same place regardless.

@isaacs I've tried adding the preliminary \n char, but it seems that changes the behavior of normal, synchronously caught exceptions. i.e. all repl errors are now being caught by the domain. I'm not sure if this is the desired behavior or not...

AlexeyKupershtokh
AlexeyKupershtokh Mar 17, 2013

@TooTallNate ah. But by the time async command arises we could have typed a new command. Why should repl delete it?

isaacs
isaacs Mar 17, 2013

I have a patch to fix this. Will send in a few days.

On Saturday, March 16, 2013, Alexey Kupershtokh wrote:

In lib/repl.js:

@@ -199,6 +200,13 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
self.displayPrompt();
});

  • var replDomain = domain.create();
  • replDomain.on('error', function(e) {
  • self.outputStream.write((e.stack || e) + '\n');
  • self.bufferedCommand = '';
  • self.displayPrompt();

@TooTallNate https://github.com/TooTallNate ah. But by the time async
command arises we could have typed a new command. Why should repl delete it?


Reply to this email directly or view it on GitHubhttps://github.com/joyent/node/pull/4491/files#r3404752
.

+ });
+
rli.on('line', function(cmd) {
sawSIGINT = false;
var skipCatchall = false;
@@ -245,21 +253,24 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
// First we attempt to eval as expression with parens.
// This catches '{a : 1}' properly.
- self.eval('(' + evalCmd + ')',
- self.context,
- 'repl',
- function(e, ret) {
- if (e && !isSyntaxError(e)) return finish(e);
-
- if (typeof ret === 'function' &&
- /^[\r\n\s]*function/.test(evalCmd) ||
- e) {
- // Now as statement without parens.
- self.eval(evalCmd, self.context, 'repl', finish);
- } else {
- finish(null, ret);
- }
- });
+
+ replDomain.run(function() {
+ self.eval('(' + evalCmd + ')',
+ self.context,
+ 'repl',
+ function(e, ret) {
+ if (e && !isSyntaxError(e)) return finish(e);
+
+ if (typeof ret === 'function' &&
+ /^[\r\n\s]*function/.test(evalCmd) ||
+ e) {
+ // Now as statement without parens.
+ self.eval(evalCmd, self.context, 'repl', finish);
+ } else {
+ finish(null, ret);
+ }
+ });
+ });
} else {
finish(null);
@@ -0,0 +1,55 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var assert = require('assert');
+var util = require('util');
+var repl = require('repl');
+
+// A stream to push an array into a REPL
+function ArrayStream() {
+ this.run = function (data) {
+ var self = this;
+ data.forEach(function (line) {
+ self.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 () {};
+
+var putIn = new ArrayStream;
+var testMe = repl.start('', putIn, null, false);
+
+var handled = false;
+
+putIn.write = function(data) {
+ if (/Error: Async error thrown./.test(data)) {
+ handled = true;
+ }
+};
+putIn.run(['process.nextTick(function() { throw new Error("Async error thrown."); })']);
+
+process.on('exit', function() {
+ assert.equal(handled, true);
+});