From 8a2d3d4669546272da870600831f71eec36bdc71 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 16 Feb 2023 00:06:45 +0000 Subject: [PATCH] Pass around SQL errors slightly better. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4180 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.js.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ssb.js.c b/src/ssb.js.c index 41537bee..35affc0f 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -345,6 +345,7 @@ typedef struct _sql_work_t JSValue callback; JSValue promise[2]; int result; + char* error; } sql_work_t; static void _tf_ssb_sql_append(uint8_t** rows, size_t* rows_count, const void* data, size_t size) @@ -359,7 +360,8 @@ static void _tf_ssb_sqlAsync_work(uv_work_t* work) sql_work_t* sql_work = work->data; sqlite3* db = tf_ssb_acquire_db_reader(sql_work->ssb); sqlite3_stmt* statement = NULL; - if (sqlite3_prepare(db, sql_work->query, -1, &statement, NULL) == SQLITE_OK) + sql_work->result = sqlite3_prepare(db, sql_work->query, -1, &statement, NULL); + if (sql_work->result == SQLITE_OK) { const uint8_t* p = sql_work->binds; int column = 0; @@ -443,7 +445,7 @@ static void _tf_ssb_sqlAsync_work(uv_work_t* work) } else { - printf("prepare failed\n"); + sql_work->error = tf_strdup(sqlite3_errmsg(db)); } tf_ssb_release_db_reader(sql_work->ssb, db); } @@ -513,14 +515,28 @@ static void _tf_ssb_sqlAsync_after_work(uv_work_t* work, int status) tf_free(sql_work->binds); tf_free(sql_work->rows); - JSValue result = JS_Call(context, sql_work->promise[0], JS_UNDEFINED, 0, NULL); - tf_util_report_error(context, result); + JSValue result = JS_UNDEFINED; + if (sql_work->result == SQLITE_OK) + { + result = JS_Call(context, sql_work->promise[0], JS_UNDEFINED, 0, NULL); + tf_util_report_error(context, result); + } + else + { + JSValue error = JS_ThrowInternalError(context, "SQL Error %s: %s", sql_work->error, sql_work->query); + JSValue exception = JS_GetException(context); + result = JS_Call(context, sql_work->promise[1], JS_UNDEFINED, 1, &exception); + tf_util_report_error(context, result); + JS_FreeValue(context, exception); + JS_FreeValue(context, error); + } JS_FreeValue(context, result); JS_FreeValue(context, sql_work->promise[0]); JS_FreeValue(context, sql_work->promise[1]); JS_FreeValue(context, sql_work->callback); JS_FreeCString(context, sql_work->query); + tf_free(sql_work->error); tf_free(sql_work); } @@ -599,6 +615,7 @@ static JSValue _tf_ssb_sqlAsync(JSContext* context, JSValueConst this_val, int a JS_FreeValue(context, error_value); JS_FreeCString(context, query); tf_free(work->binds); + tf_free(work->error); tf_free(work); } return result;