repl: fix freeze if util.inspect throws & clean up error handling a bit
If an error is thrown during the readline keypress
event, the state of the streaming escape sequence decoder (added in https://github.com/nodejs/io.js/pull/1601) will become corrupted. This will render the REPL (and possibly your whole terminal) completely unusable. This can be triggered by an object with a custom inspect
method that throws.
Originally I modified the escape sequence decoder to avoid corruption but it proved difficult to test because the synchronous throw in keypress
would end up corrupting the state of the mock streams I was using to test (although for whatever reason it would work with the real TTY streams). Felt too fragile to commit.
Instead, I decided that none of the eval machinery should ever throw. This PR adds an extra try/catch around self.writer
(which is util.inspect
by default) and makes some minor cleanups to make error handling more consistent.
Note: our test suite currently forbids running any of the REPL machinery in a different tick, which would sidestep most, if not all, of these problems.
cc @rlidwka