Error handle

This commit is contained in:
anseki 2013-12-18 02:18:39 +09:00
parent f2455207cd
commit 1a136eeca7
3 changed files with 21 additions and 2 deletions

View file

@ -51,6 +51,10 @@ readlineSync.setPrompt(encoding)
Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'. Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
## Note
The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms.
## Release History ## Release History
* 2013-12-18 v0.2.1 Error handle for the environment which don't support interactively reading from stdin.
* 2013-08-30 v0.2.0 Rewrite exporting methods. * 2013-08-30 v0.2.0 Rewrite exporting methods.
* 2013-08-29 v0.1.0 Initial release. * 2013-08-29 v0.1.0 Initial release.

View file

@ -22,7 +22,22 @@ var _readlineSync = function(display) {
if (display) { stdout.write(display, encoding); } if (display) { stdout.write(display, encoding); }
stdin.resume(); stdin.resume();
while ((rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)) > 0) { while (true) {
rsize = 0;
try {
rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE);
} catch (e) {
if (e.code === 'EAGAIN') {
// Error: EAGAIN, resource temporarily unavailable
if (display) { stdout.write('\n', encoding); } // Next of prompt line.
console.error('Error: This machine don\'t support interactively reading from stdin.');
process.exit(1);
} else if (e.code === 'EOF') {
break;
}
throw e;
}
if (rsize === 0) { break; }
input += buffer.toString(encoding, 0, rsize); input += buffer.toString(encoding, 0, rsize);
if (/[\r\n]$/.test(input)) { break; } if (/[\r\n]$/.test(input)) { break; }
} }

View file

@ -1,7 +1,7 @@
{ {
"name": "readline-sync", "name": "readline-sync",
"description": "Synchronous Readline", "description": "Synchronous Readline",
"version": "0.2.0", "version": "0.2.1",
"homepage": "https://github.com/anseki/readline-sync", "homepage": "https://github.com/anseki/readline-sync",
"author": { "author": {
"name": "anseki" "name": "anseki"