From eca0e9eb20c6f1a07fcc14bcefd4e24c7a71f83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Thu, 24 Feb 2022 21:53:43 +0100 Subject: [PATCH] repl: fix abruptly exiting on completion errors --- src/js/repl.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/js/repl.js b/src/js/repl.js index ecd121be..9b0cebbc 100644 --- a/src/js/repl.js +++ b/src/js/repl.js @@ -149,9 +149,13 @@ window.addEventListener('unhandledrejection', event => { var buf = new Uint8Array(4096); var nread; while (true) { - nread = await tjs.stdin.read(buf); - for(var i = 0; i < nread; i++) - handle_byte(buf[i]); + try { + nread = await tjs.stdin.read(buf); + for(var i = 0; i < nread; i++) + handle_byte(buf[i]); + } catch (error) { + dump_error(error); + } } } @@ -1008,18 +1012,22 @@ window.addEventListener('unhandledrejection', event => { /* set the last result */ g._ = result; } catch (error) { - stdout_write(colors[styles.error_msg]); - if (error instanceof Error) { - stdout_write(error); - stdout_write("\n"); - stdout_write(error.stack); - } else { - stdout_write("Throw: "); - stdout_write(error); - } + dump_error(error); + } + } + + function dump_error(error) { + stdout_write(colors[styles.error_msg]); + if (error instanceof Error) { + stdout_write(error); stdout_write("\n"); - stdout_write(colors.none); + stdout_write(error.stack); + } else { + stdout_write("Throw: "); + stdout_write(error); } + stdout_write("\n"); + stdout_write(colors.none); } function cmd_start() {