summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Chimento <philip.chimento@gmail.com>2018-10-04 09:04:00 -0700
committerPhilip Chimento <philip.chimento@gmail.com>2018-10-22 10:16:31 -0400
commit0decd7ed454ec90acd2456236a34f701c3fb0852 (patch)
tree40c59fc9ef6328e16d42b92709af5f2641b8d3e0
parente242514409cbb654998bcb68126cc5b7be048ba2 (diff)
downloadgjs-0decd7ed454ec90acd2456236a34f701c3fb0852.tar.gz
console: Refactor AutoReportException for non-object exceptions
Previously, in the interactive interpreter, throwing a non-object exception (for example, `throw 'foo';`) would crash when trying to print the exception value in the console. This changes AutoReportException so that exceptions are handled better when they are not objects.
-rw-r--r--modules/console.cpp31
1 files changed, 21 insertions, 10 deletions
diff --git a/modules/console.cpp b/modules/console.cpp
index dcbe2089..85a412af 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -52,6 +52,8 @@
#include <glib.h>
#include <glib/gprintf.h>
+#include <string>
+
#include "console.h"
#include "gjs/context.h"
#include "gjs/context-private.h"
@@ -142,6 +144,20 @@ gjs_console_warning_reporter(JSContext *cx, JSErrorReport *report)
class AutoReportException {
JSContext *m_cx;
+ JSErrorReport* error_from_exception_value(JS::HandleValue v_exn) const {
+ if (!v_exn.isObject())
+ return nullptr;
+ JS::RootedObject exn(m_cx, &v_exn.toObject());
+ return JS_ErrorFromException(m_cx, exn);
+ }
+
+ JSObject* stack_from_exception_value(JS::HandleValue v_exn) const {
+ if (!v_exn.isObject())
+ return nullptr;
+ JS::RootedObject exn(m_cx, &v_exn.toObject());
+ return ExceptionStackOrNull(exn);
+ }
+
public:
explicit AutoReportException(JSContext *cx) : m_cx(cx) {}
@@ -153,22 +169,17 @@ public:
JS::RootedValue v_exn(m_cx);
(void) JS_GetPendingException(m_cx, &v_exn);
- JS::RootedObject exn(m_cx, &v_exn.toObject());
- JSErrorReport *report = JS_ErrorFromException(m_cx, exn);
+ JSErrorReport* report = error_from_exception_value(v_exn);
if (report) {
g_assert(!JSREPORT_IS_WARNING(report->flags));
gjs_console_print_error(report);
} else {
- JS::RootedString message(m_cx, JS::ToString(m_cx, v_exn));
- if (!message) {
- g_printerr("(could not convert thrown exception to string)\n");
- } else {
- GjsAutoJSChar message_utf8 = JS_EncodeStringToUTF8(m_cx, message);
- g_printerr("%s\n", message_utf8.get());
- }
+ GjsAutoChar string = gjs_value_debug_string(m_cx, v_exn);
+ g_printerr("error: %s\n", string.get());
+ return;
}
- JS::RootedObject stack(m_cx, ExceptionStackOrNull(exn));
+ JS::RootedObject stack(m_cx, stack_from_exception_value(v_exn));
if (stack) {
GjsAutoChar stack_str = gjs_format_stack_trace(m_cx, stack);
if (!stack_str)