Skip to content

Commit d411020

Browse files
feat: add warning reporting functionality to error handler and no longer crash if JS context destroyed when calling function
1 parent d19ab0d commit d411020

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/shell/error_handler.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ void mb_shell::install_error_handlers() {
110110
g_previous_sigabrt_handler = signal(SIGABRT, handle_sigabrt);
111111
}
112112

113+
void mb_shell::report_warning(const char* message) {
114+
sentry_value_t event = sentry_value_new_event();
115+
sentry_value_set_by_key(event, "level", sentry_value_new_string("warning"));
116+
117+
sentry_value_t exc = sentry_value_new_exception("Warning", message);
118+
sentry_value_t stacktrace = sentry_value_new_stacktrace(nullptr, 0);
119+
sentry_value_set_by_key(exc, "stacktrace", stacktrace);
120+
sentry_value_set_by_key(event, "exception", sentry_value_new_list());
121+
sentry_value_append(sentry_value_get_by_key(event, "exception"), exc);
122+
123+
sentry_capture_event(event);
124+
}
125+
113126
void mb_shell::cleanup_error_handlers() {
114127
if (g_previous_sigabrt_handler) {
115128
signal(SIGABRT, g_previous_sigabrt_handler);

src/shell/error_handler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#pragma once
2+
#include <fmt/format.h>
3+
24
namespace mb_shell {
35
void install_error_handlers();
46
void cleanup_error_handlers();
7+
void report_warning(const char* message);
8+
9+
template<typename... Args>
10+
void report_warning(fmt::format_string<Args...> fmt, Args&&... args) {
11+
report_warning(fmt::format(fmt, std::forward<Args>(args)...).c_str());
12+
}
513
}

src/shell/script/quickjspp.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "quickjs.h"
4+
#include "../error_handler.h"
45

56
#include <algorithm>
67
#include <atomic>
@@ -1961,8 +1962,14 @@ struct js_traits<std::function<R(Args...)>, int> {
19611962
return
19621963
[jsfun_obj = Value{weakFromContext(ctx), JS_DupValue(ctx, fun_obj)},
19631964
weak](Args... args) -> R {
1964-
if (weak.expired())
1965-
throw qjs_context_destroyed_exception{};
1965+
if (weak.expired()) {
1966+
mb_shell::report_warning("JS context destroyed when calling function");
1967+
if constexpr (!std::is_void_v<R>) {
1968+
return R{};
1969+
} else {
1970+
return;
1971+
}
1972+
}
19661973
auto &ctx = Context::get(jsfun_obj.ctx);
19671974
std::promise<std::expected<JSValue, exception>> promise;
19681975
auto future = promise.get_future();

0 commit comments

Comments
 (0)