Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

repl: handle async error in repl #4491

Closed
wants to merge 1 commit into from

7 participants

Ryunosuke SATO Ben Noordhuis Nathan Rajlich Bradley Meck Nodejs Jenkins isaacs Alexey Kupershtokh
Ryunosuke SATO

I think repl should handle any error thrown.
But now async errors are unhandled in repl.

When async error thrown, process is exited with status 1.

$ node
> process.nextTick(function() { throw new Error(); });
> 
repl:1
process.nextTick(function() { throw new Error(); });
                                    ^
Error
    at repl:1:37
    at process._tickCallback (node.js:386:13)
    at process._makeCallback (node.js:304:15)

I supported async error handling.

Ben Noordhuis

/cc @TooTallNate - you should probably review this.

Nathan Rajlich TooTallNate was assigned
Nathan Rajlich
Owner

I actually wrote a patch similar to this in the past but @isaacs passed on it because domains should be an opt-in thing, and not activated simply when entering the REPL.

Consider a production server, which allows remote REPLs for debugging live instances. With this patch, starting a REPL would now cause a negative performance impact (if the server didn't previously use domains).

Ryunosuke SATO

Thank you for your review @TooTallNate .
I have understood that it should be depend on user whether using domains or not in REPL.

But what's really bothering me is that the process started by node -i is exited in suddenly when it catches async error.
Such as http.createServer().listen(3000) throws async error and it is well-used code.

I hope that async errors are handled in node -i.

For example, the following idea solves this issue.
REPL allows domains for opt-in thing (that is disabled in default) and the process started with node -i enable this.

How do you think about my idea?

Nathan Rajlich
Owner

Well the -i flag is for forcing the REPL open, so I don't think domains should be defaulted to be on there.

I would be open for an option and/or a function to enable domain-mode in a REPL instance but it should be defaulted to off always.

Bradley Meck

@TooTallNate I think bootstrapping node without args REPL would seem sane, any objections to the CLI REPL changing since you should never do that for production?

Nodejs Jenkins

Can one of the admins verify this patch?

isaacs
Owner

Sorry, didn't see this. Did almost the same thing, but with a different test, in c0721bc

isaacs isaacs closed this
Alexey Kupershtokh AlexeyKupershtokh commented on the diff
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();

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

isaacs Owner
isaacs added a note

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.

Nathan Rajlich Owner

@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...

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

isaacs Owner
isaacs added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Ryunosuke SATO

I'm glad to hear that the feature I wanted was landed in master!
Thanks @isaacs.

Ryunosuke SATO tricknotes deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2012
  1. Ryunosuke SATO
This page is out of date. Refresh to see the latest.
Showing with 81 additions and 15 deletions.
  1. +26 −15 lib/repl.js
  2. +55 −0 test/simple/test-repl-handle-async-error.js
41 lib/repl.js
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();

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

isaacs Owner
isaacs added a note

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.

Nathan Rajlich Owner

@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...

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

isaacs Owner
isaacs added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ });
+
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);
55 test/simple/test-repl-handle-async-error.js
View
@@ -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);
+});
Something went wrong with that request. Please try again.