Change Error object thrown.

This commit is contained in:
anseki 2015-03-06 15:23:32 +09:00
parent a6039bcf36
commit b27ad119ae

View file

@ -12,9 +12,9 @@ var
IS_WIN = process.platform === 'win32',
SHELL_PATH = IS_WIN ? 'cmd.exe' : '/bin/sh',
SHELL_CMD = __dirname + (IS_WIN ? '\\read.bat' : '/read.sh'),
ALGORITHM_CIPHER = 'aes-256-cbc',
ALGORITHM_HASH = 'sha256',
DEFAULT_ERR_MSG = 'The platform doesn\'t support interactive reading',
fs = require('fs'),
childProc = require('child_process'),
@ -40,9 +40,9 @@ function _readlineSync(display, options) {
res = _readlineShell(options);
if (res.error) {
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
throw newError(res.props);
throw res.error;
}
input = res.stdout;
input = res.input;
} else {
@ -59,9 +59,9 @@ function _readlineSync(display, options) {
res = _readlineShell(options);
if (res.error) {
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
throw newError(res.props);
throw res.error;
}
input += res.stdout;
input += res.input;
break;
}
@ -76,17 +76,6 @@ function _readlineSync(display, options) {
return options.noTrim ? input.replace(/[\r\n]+$/, '') : input.trim();
}
function newError(props) {
var err = new Error('The platform doesn\'t support interactive reading from stdin'),
key;
if (props) {
for (key in props) {
if (props.hasOwnProperty(key)) { err[key] = props[key]; }
}
}
return err;
}
function _readlineShell(options) {
var args = [], res = {},
execOptions = {
@ -102,18 +91,19 @@ function _readlineShell(options) {
stdin.pause(); // re-start in child process
if (childProc.execFileSync) {
try {
res.stdout = childProc.execFileSync(SHELL_PATH,
res.input = childProc.execFileSync(SHELL_PATH,
(IS_WIN ? ['/C', SHELL_CMD] : [SHELL_CMD]).concat(args), execOptions);
} catch (e) { // non-zero exit code
res = {error: true, props: {
method: 'execFileSync',
command: SHELL_CMD, args: args, stderr: e.stderr.trim()
}};
res.error = new Error(DEFAULT_ERR_MSG);
res.error.method = 'execFileSync';
res.error.command = SHELL_CMD;
res.error.args = args;
res.error.shellMessage = e.stderr.trim();
}
} else {
res = _execSyncByFile(args, execOptions);
}
if (!res.error) { res.stdout = res.stdout.replace(/^'|'$/g, ''); }
if (!res.error) { res.input = res.input.replace(/^'|'$/g, ''); }
return res;
}
@ -180,15 +170,15 @@ function _execSyncByFile(args, execOptions) {
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {}
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
res.stdout =
res.input =
decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), 'hex', encoding) +
decipher.final(encoding);
} else {
res = {error: true, props: {
method: '_execSyncByFile',
command: SHELL_CMD, args: args,
stderr: fs.readFileSync(pathStderr, {encoding: encoding}).trim()
}};
res.error = new Error(DEFAULT_ERR_MSG);
res.error.method = '_execSyncByFile';
res.error.command = SHELL_CMD;
res.error.args = args;
res.error.shellMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim();
}
fs.unlinkSync(pathStdout);