Skip to content

Commit

Permalink
repl: don't crash if cannot open history file
Browse files Browse the repository at this point in the history
Previously, if we are unable to open the history file, an error would
be thrown. Now, print an error message that we could not open
the history file, but don't fail.

Fixes: #3610
PR-URL: #3630
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
evanlucas authored and rvagg committed Dec 4, 2015
1 parent 19dde92 commit f5a1ab7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/internal/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {

function oninit(err, hnd) {
if (err) {
return ready(err);
// Cannot open history file.
// Don't crash, just don't persist history.
repl._writeToOutput('\nError: Could not open history file.\n' +
'REPL session history will not be persisted.\n');
repl._refreshLine();
debug(err.stack);

repl._historyPrev = _replHistoryMessage;
repl.resume();
return ready(null, repl);
}
fs.close(hnd, onclose);
}
Expand Down
9 changes: 9 additions & 0 deletions test/sequential/test-repl-persistent-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ const convertMsg = '\nConverting old JSON repl history to line-separated ' +
path.join(common.tmpDir, '.node_repl_history') + '.\n';
const homedirErr = '\nError: Could not get the home directory.\n' +
'REPL session history will not be persisted.\n';
const replFailedRead = '\nError: Could not open history file.\n' +
'REPL session history will not be persisted.\n';
// File paths
const fixtures = path.join(common.testDir, 'fixtures');
const historyFixturePath = path.join(fixtures, '.node_repl_history');
const historyPath = path.join(common.tmpDir, '.fixture_copy_repl_history');
const historyPathFail = path.join(common.tmpDir, '.node_repl\u0000_history');
const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json');
const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json');
const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history');
Expand Down Expand Up @@ -147,6 +150,12 @@ const tests = [{
test: [UP, UP, UP, CLEAR],
expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt]
},
{
env: { NODE_REPL_HISTORY: historyPathFail,
NODE_REPL_HISTORY_SIZE: 1 },
test: [UP],
expected: [prompt, replFailedRead, prompt, replDisabled, prompt]
},
{ // Make sure this is always the last test, since we change os.homedir()
before: function mockHomedirFailure() {
// Mock os.homedir() failure
Expand Down

0 comments on commit f5a1ab7

Please sign in to comment.