Skip to content

Commit

Permalink
Log unhandled errors / promise rejections to the debug log file
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Nov 11, 2023
1 parent f51c9e5 commit 2055162
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-ligers-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Log unhandled errors / promise rejections to the debug log file
31 changes: 29 additions & 2 deletions source/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ void print_js_error(JSContext *ctx)
const char *exception_str = JS_ToCString(ctx, exception_val);
printf("%s\n", exception_str);
fprintf(stderr, "%s\n", exception_str);
fflush(stderr);
JS_FreeCString(ctx, exception_str);

JSValue stack_val = JS_GetPropertyStr(ctx, exception_val, "stack");
const char *stack_str = JS_ToCString(ctx, stack_val);
printf("%s\n", stack_str);
fprintf(stderr, "%s\n", stack_str);
fflush(stderr);

JS_FreeCString(ctx, stack_str);
JS_FreeValue(ctx, exception_val);
JS_FreeValue(ctx, stack_val);
Expand All @@ -23,6 +22,20 @@ void nx_emit_error_event(JSContext *ctx)
{
JSValue exception_val = JS_GetException(ctx);
nx_context_t *nx_ctx = JS_GetContextOpaque(ctx);

// Print the error to stderr so that it ends up in the log file
const char *exception_str = JS_ToCString(ctx, exception_val);
fprintf(stderr, "Uncaught %s\n", exception_str);
JS_FreeCString(ctx, exception_str);

JSValue stack_val = JS_GetPropertyStr(ctx, exception_val, "stack");
if (!JS_IsUndefined(stack_val)) {
const char *stack_str = JS_ToCString(ctx, stack_val);
fprintf(stderr, "%s\n", stack_str);
JS_FreeCString(ctx, stack_str);
JS_FreeValue(ctx, stack_val);
}

JSValueConst args[] = {exception_val};
JSValue ret_val = JS_Call(ctx, nx_ctx->error_handler, JS_NULL, 1, args);
if (JS_IsException(ret_val))
Expand Down Expand Up @@ -52,6 +65,20 @@ void nx_promise_rejection_handler(JSContext *ctx, JSValueConst promise,
JS_BOOL is_handled, void *opaque)
{
nx_context_t *nx_ctx = JS_GetContextOpaque(ctx);

// Print the error to stderr so that it ends up in the log file
const char *exception_str = JS_ToCString(ctx, reason);
fprintf(stderr, "Uncaught (in promise) %s\n", exception_str);
JS_FreeCString(ctx, exception_str);

JSValue stack_val = JS_GetPropertyStr(ctx, reason, "stack");
if (!JS_IsUndefined(stack_val)) {
const char *stack_str = JS_ToCString(ctx, stack_val);
fprintf(stderr, "%s\n", stack_str);
JS_FreeCString(ctx, stack_str);
JS_FreeValue(ctx, stack_val);
}

JSValueConst args[] = {promise, reason};
JSValue ret_val = JS_Call(ctx, nx_ctx->unhandled_rejection_handler, JS_NULL, 2, args);
if (JS_IsException(ret_val))
Expand Down

0 comments on commit 2055162

Please sign in to comment.