Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Aug 17, 2019
1 parent 7e7080f commit e3492d0
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions lib/node_modules/@stdlib/repl/lib/commands/rerun.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,29 @@ function command( repl ) {
function onCommand( arg ) {
var nargs;
var len;
var FLG;
var err;

len = repl._history.length;
if ( len === 0 ) {
repl._ostream.write( 'History is empty. No commands to run.\n' );
return;
}
nargs = arguments.length;
if ( isString( arg ) ) {
FLG = 1;
} else if ( isNonNegativeInteger( arg ) ) {
FLG = 2;
} else if ( isRegExp( arg ) ) {
FLG = 3;
} else if ( isNonNegativeIntegerArray( arg ) ) {
FLG = 4;
} else {
err = new TypeError( 'invalid argument. Must provide a string, regular expression, nonnegative integer, or an array of nonnegative integers. Value: `' + arg + '`.' );
debug( 'Error: %s', err.message );
repl._ostream.write( 'Error: '+err.message+'\n' );
return;
}

// Why defer? In order to allow the `rerun()` command to finish before actually evaluating the commands to rerun, thus ensuring that commands are run as if a user manually enters them...
repl.once( 'drain', onFinish );
Expand All @@ -77,7 +93,6 @@ function command( repl ) {
* @returns {void}
*/
function onFinish() {
var err;
var n;
var i;
var j;
Expand All @@ -87,11 +102,11 @@ function command( repl ) {
repl._rli.write( repl._history[ len-2 ]+'\n' );
return;
}
if ( isString( arg ) ) {
if ( FLG === 1 ) {
// TODO: subsequence string parsing (use an iterator!!!)
return;
}
if ( isNonNegativeInteger( arg ) ) {
if ( FLG === 2 ) {
i = len - (3*arg);
if ( i < 0 ) {
n = len / 3;
Expand All @@ -111,7 +126,7 @@ function command( repl ) {
}
return;
}
if ( isRegExp( arg ) ) {
if ( FLG === 3 ) {
// Scan the history for the most recent command matching the regular expression...
for ( i = len-2; i >= 0; i -= 3 ) {
if ( arg.test( repl._history[ i ] ) ) {
Expand All @@ -121,26 +136,20 @@ function command( repl ) {
}
return;
}
if ( isNonNegativeIntegerArray( arg ) ) {
n = arg.length;
for ( i = 0; i < n; i++ ) {
// Scan the history for the command identifier...
for ( j = 0; j < len; j += 3 ) {
if ( arg[ i ] === repl._history[ j ] ) {
repl._rli.write( repl._history[ j+1 ]+'\n' );
if ( i < n-1 ) {
displayPrompt( repl, false );
}
break;
// Case: FLG === 4
n = arg.length;
for ( i = 0; i < n; i++ ) {
// Scan the history for the command identifier...
for ( j = 0; j < len; j += 3 ) {
if ( arg[ i ] === repl._history[ j ] ) {
repl._rli.write( repl._history[ j+1 ]+'\n' );
if ( i < n-1 ) {
displayPrompt( repl, false );
}
break;
}
}
return;
}
// FIXME: this error handling needs to be done BEFORE drain!
err = new TypeError( 'invalid argument. Must provide a string, regular expression, nonnegative integer, or an array of nonnegative integers. Value: `' + arg + '`.' );
debug( 'Error: %s', err.message );
repl._ostream.write( 'Error: '+err.message+'\n' );
}
}
}
Expand Down

0 comments on commit e3492d0

Please sign in to comment.