Skip to content

Commit 74e72db

Browse files
James Prestwoodgrgustaf
James Prestwood
authored andcommitted
[callbacks] Print errors for our own callbacks (zephyrproject-rtos#648)
- There is still not support for handling these errors, but at least printing them is better than nothing Signed-off-by: James Prestwood <[email protected]>
1 parent 0f82d1c commit 74e72db

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/zjs_callbacks.c

+50-2
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,53 @@ void print_callbacks(void)
388388
#define print_callbacks() do {} while (0)
389389
#endif
390390

391+
#define MAX_ERROR_NAME_LENGTH 32
392+
#define MAX_ERROR_MESSAGE_LENGTH 128
393+
394+
static void print_error_message(jerry_value_t error)
395+
{
396+
uint32_t size;
397+
char* message = NULL;
398+
jerry_value_t err_name = zjs_get_property(error, "name");
399+
if (!jerry_value_is_string(err_name)) {
400+
ERR_PRINT("error did not have name\n");
401+
jerry_release_value(err_name);
402+
// we should never get here.
403+
return;
404+
}
405+
jerry_value_t err_msg = zjs_get_property(error, "message");
406+
if (!jerry_value_is_string(err_msg)) {
407+
ERR_PRINT("error did not have message\n");
408+
jerry_release_value(err_name);
409+
jerry_release_value(err_msg);
410+
// we should never get here.
411+
return;
412+
}
413+
414+
size = MAX_ERROR_NAME_LENGTH;
415+
char name[size];
416+
417+
zjs_copy_jstring(err_name, name, &size);
418+
if (!size) {
419+
ERR_PRINT("name length is too long\n");
420+
jerry_release_value(err_name);
421+
jerry_release_value(err_msg);
422+
return;
423+
}
424+
jerry_release_value(err_name);
425+
426+
message = zjs_alloc_from_jstring(err_msg, NULL);
427+
428+
jerry_release_value(err_msg);
429+
430+
if (message) {
431+
ERR_PRINT("Uncaught exception: %s: %s\n", name, message);
432+
zjs_free(message);
433+
} else {
434+
ERR_PRINT("Uncaught exception: %s\n", name);
435+
}
436+
}
437+
391438
void zjs_call_callback(zjs_callback_id id, void* data, uint32_t sz)
392439
{
393440
if (id <= cb_size && cb_map[id]) {
@@ -398,13 +445,13 @@ void zjs_call_callback(zjs_callback_id id, void* data, uint32_t sz)
398445
if (GET_JS_TYPE(cb_map[id]->flags) == JS_TYPE_SINGLE) {
399446
ret_val = jerry_call_function(cb_map[id]->js_func, cb_map[id]->this, data, sz);
400447
if (jerry_value_has_error_flag(ret_val)) {
401-
DBG_PRINT("callback %d returned an error for function\n", id);
448+
print_error_message(ret_val);
402449
}
403450
} else if (GET_JS_TYPE(cb_map[id]->flags) == JS_TYPE_LIST) {
404451
for (i = 0; i < cb_map[id]->num_funcs; ++i) {
405452
ret_val = jerry_call_function(cb_map[id]->func_list[i], cb_map[id]->this, data, sz);
406453
if (jerry_value_has_error_flag(ret_val)) {
407-
DBG_PRINT("callback %d returned an error for function[%i]\n", id, i);
454+
print_error_message(ret_val);
408455
}
409456
}
410457
}
@@ -418,6 +465,7 @@ void zjs_call_callback(zjs_callback_id id, void* data, uint32_t sz)
418465
if (GET_ONCE(cb_map[id]->flags)) {
419466
zjs_remove_callback(id);
420467
}
468+
jerry_release_value(ret_val);
421469
} else if (GET_TYPE(cb_map[id]->flags) == CALLBACK_TYPE_C && cb_map[id]->function) {
422470
cb_map[id]->function(cb_map[id]->handle, data);
423471
}

0 commit comments

Comments
 (0)