Pass around SQL errors slightly better.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4180 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-02-16 00:06:45 +00:00
parent 1741403206
commit 8a2d3d4669

View File

@ -345,6 +345,7 @@ typedef struct _sql_work_t
JSValue callback; JSValue callback;
JSValue promise[2]; JSValue promise[2];
int result; int result;
char* error;
} sql_work_t; } sql_work_t;
static void _tf_ssb_sql_append(uint8_t** rows, size_t* rows_count, const void* data, size_t size) 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; sql_work_t* sql_work = work->data;
sqlite3* db = tf_ssb_acquire_db_reader(sql_work->ssb); sqlite3* db = tf_ssb_acquire_db_reader(sql_work->ssb);
sqlite3_stmt* statement = NULL; 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; const uint8_t* p = sql_work->binds;
int column = 0; int column = 0;
@ -443,7 +445,7 @@ static void _tf_ssb_sqlAsync_work(uv_work_t* work)
} }
else else
{ {
printf("prepare failed\n"); sql_work->error = tf_strdup(sqlite3_errmsg(db));
} }
tf_ssb_release_db_reader(sql_work->ssb, 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->binds);
tf_free(sql_work->rows); tf_free(sql_work->rows);
JSValue result = JS_Call(context, sql_work->promise[0], JS_UNDEFINED, 0, NULL); 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); 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, result);
JS_FreeValue(context, sql_work->promise[0]); JS_FreeValue(context, sql_work->promise[0]);
JS_FreeValue(context, sql_work->promise[1]); JS_FreeValue(context, sql_work->promise[1]);
JS_FreeValue(context, sql_work->callback); JS_FreeValue(context, sql_work->callback);
JS_FreeCString(context, sql_work->query); JS_FreeCString(context, sql_work->query);
tf_free(sql_work->error);
tf_free(sql_work); 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_FreeValue(context, error_value);
JS_FreeCString(context, query); JS_FreeCString(context, query);
tf_free(work->binds); tf_free(work->binds);
tf_free(work->error);
tf_free(work); tf_free(work);
} }
return result; return result;