Consolidate error handling until util, too.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3682 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2021-11-03 22:28:25 +00:00
parent fde7fb4270
commit 2fb7fceb0c
6 changed files with 81 additions and 92 deletions

View File

@ -247,9 +247,9 @@ static void _export_record_release_for_task(tf_task_t* task, taskid_t task_id)
}
}
static void _tf_task_send_error_to_parent(tf_task_t* task, JSValue error)
void tf_task_send_error_to_parent(tf_task_t* task, JSValue error)
{
if (task->_parent)
if (task && task->_parent)
{
void* buffer = NULL;
size_t size = 0;
@ -259,36 +259,6 @@ static void _tf_task_send_error_to_parent(tf_task_t* task, JSValue error)
}
}
void tf_task_report_error(tf_task_t* task, JSValue error)
{
JSContext* context = task->_context;
if (JS_IsError(context, error))
{
const char* value = JS_ToCString(context, error);
printf("ERROR: %s\n", value);
JS_FreeCString(context, value);
JSValue stack = JS_GetPropertyStr(context, error, "stack");
if (!JS_IsUndefined(stack))
{
const char* stack_str = JS_ToCString(context, stack);
printf("%s\n", stack_str);
JS_FreeCString(context, stack_str);
}
JS_FreeValue(context, stack);
_tf_task_send_error_to_parent(task, error);
}
else if (JS_IsException(error))
{
error = JS_GetException(context);
const char* value = JS_ToCString(context, error);
printf("Exception: %s\n", value);
JS_FreeCString(context, value);
_tf_task_send_error_to_parent(task, error);
}
}
static const char* _task_loadFile(const char* fileName)
{
char* result = NULL;
@ -348,7 +318,7 @@ static void _task_timeoutCallback(uv_timer_t* handle)
JS_NULL,
0,
NULL);
tf_task_report_error(timeout->_task, result);
tf_util_report_error(timeout->_task->_context, result);
JS_FreeValue(timeout->_task->_context, result);
tf_task_run_jobs(timeout->_task);
tf_trace_end(timeout->_task->_trace);
@ -394,7 +364,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName)
if (source)
{
JSValue result = JS_Eval(task->_context, source, strlen(source), fileName, 0);
tf_task_report_error(task, result);
tf_util_report_error(task->_context, result);
if (!JS_IsError(task->_context, result) && !JS_IsException(result))
{
executed = true;
@ -514,7 +484,7 @@ JSValue _task_invokeExport_internal(tf_taskstub_t* from, tf_task_t* to, exportid
}
result = JS_Call(to->_context, function, this_val, length - 1, argument_array);
tf_trace_end(to->_trace);
tf_task_report_error(to, result);
tf_util_report_error(to->_context, result);
tf_task_run_jobs(to);
}
else
@ -563,10 +533,10 @@ static void _forward_promise(tf_task_t* from, tf_taskstub_t* to, promiseid_t pro
JSValue catch_handler = JS_NewCFunctionData(from->_context, _invokeCatch, 0, 0, 2, data);
JSValue error = JS_Call(from->_context, promise_then, result, 1, &then_handler);
tf_task_report_error(from, error);
tf_util_report_error(from->_context, error);
JS_FreeValue(from->_context, error);
error = JS_Call(from->_context, promise_catch, result, 1, &catch_handler);
tf_task_report_error(from, error);
tf_util_report_error(from->_context, error);
JS_FreeValue(from->_context, error);
tf_task_run_jobs(from);
@ -971,7 +941,7 @@ JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JS
JSValue oldExports = JS_GetPropertyStr(task->_context, global, "exports");
JS_SetPropertyStr(task->_context, global, "exports", JS_DupValue(task->_context, exports));
JSValue eval = JS_Eval(task->_context, source, strlen(source), path, 0);
tf_task_report_error(task, eval);
tf_util_report_error(task->_context, eval);
tf_task_run_jobs(task);
if (JS_IsError(task->_context, eval) ||
JS_IsException(eval))
@ -1008,7 +978,7 @@ static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const
{
tf_trace_begin(task->_trace, "_tf_task_executeSource");
JSValue result = JS_Eval(task->_context, source, strlen(source), name, 0);
tf_task_report_error(task, result);
tf_util_report_error(task->_context, result);
if (!*task->_scriptName)
{
snprintf(task->_scriptName, sizeof(task->_scriptName), "%s", name);
@ -1046,7 +1016,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int
JSValue oldExports = JS_GetPropertyStr(context, global, "exports");
JS_SetPropertyStr(context, global, "exports", JS_DupValue(context, exports));
JSValue result = JS_Eval(context, source, length, name, 0);
tf_task_report_error(task, result);
tf_util_report_error(context, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
tf_task_run_jobs(task);
@ -1061,7 +1031,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int
JSValue oldExports = JS_GetPropertyStr(context, global, "exports");
JS_SetPropertyStr(context, global, "exports", JS_DupValue(context, exports));
JSValue result = JS_Eval(context, source, length, name, 0);
tf_task_report_error(task, result);
tf_util_report_error(context, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
tf_task_run_jobs(task);
@ -1142,7 +1112,7 @@ void tf_task_resolve_promise(tf_task_t* task, promiseid_t promise, JSValue value
if (it)
{
JSValue result = JS_Call(task->_context, it->values[1], JS_UNDEFINED, 1, &value);
tf_task_report_error(task, result);
tf_util_report_error(task->_context, result);
JS_FreeValue(task->_context, it->values[1]);
JS_FreeValue(task->_context, it->values[2]);
JS_FreeValue(task->_context, result);
@ -1162,7 +1132,7 @@ void tf_task_reject_promise(tf_task_t* task, promiseid_t promise, JSValue value)
if (it)
{
JSValue result = JS_Call(task->_context, it->values[2], JS_UNDEFINED, 1, &value);
tf_task_report_error(task, result);
tf_util_report_error(task->_context, result);
JS_FreeValue(task->_context, it->values[1]);
JS_FreeValue(task->_context, it->values[2]);
JS_FreeValue(task->_context, result);
@ -1239,10 +1209,9 @@ static void _import_mark_func(JSRuntime* runtime, JSValueConst value, JS_MarkFun
static void _tf_task_promise_rejection_tracker(JSContext* context, JSValueConst promise, JSValueConst reason, JS_BOOL is_handled, void* user_data)
{
tf_task_t* task = user_data;
if (!is_handled)
{
tf_task_report_error(task, reason);
tf_util_report_error(context, reason);
}
}
@ -1448,7 +1417,10 @@ void tf_task_run_jobs(tf_task_t* task)
JSContext* context = NULL;
int r = JS_ExecutePendingJob(task->_runtime, &context);
JSValue result = JS_GetException(context);
tf_task_report_error(task, result);
if (context)
{
tf_util_report_error(context, result);
}
if (r < 0)
{
js_std_dump_error(context);