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