Skip to content

Commit 16082e4

Browse files
committed
Refactory on error handling of SQLBatch
- All errors refactored to be returned on the callback message instead of throwing it.
1 parent 9b25d79 commit 16082e4

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

cpp/react-native-quick-sqlite.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,19 @@ void installSequel(jsi::Runtime &rt, const char *docPath)
185185
[](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value
186186
{
187187
if(sizeof(args) < 2) {
188-
jsi::detail::throwJSError(rt, "[react-native-quick-sqlite][execSQLBatch] - Incorrect parameter count");
189-
return {};
188+
auto res = jsi::Object(rt);
189+
res.setProperty(rt, "status", jsi::Value(1));
190+
res.setProperty(rt, "message", jsi::String::createFromUtf8(rt, "[react-native-quick-sqlite][execSQLBatch] - Incorrect parameter count"));
191+
return move(res);
190192
}
191193
const string dbName = args[0].asString(rt).utf8(rt);
192194
const jsi::Value &params = args[1];
193195
if(params.isNull() || params.isUndefined())
194196
{
195-
jsi::detail::throwJSError(rt, "[react-native-quick-sqlite][execSQLBatch] - An array of SQL commands or parameters is needed");
196-
return {};
197+
auto res = jsi::Object(rt);
198+
res.setProperty(rt, "status", jsi::Value(1));
199+
res.setProperty(rt, "message", jsi::String::createFromUtf8(rt, "[react-native-quick-sqlite][execSQLBatch] - An array of SQL commands or parameters is needed"));
200+
return move(res);
197201
}
198202
int rowsAffected = 0;
199203
const jsi::Array &batchParams = params.asObject(rt).asArray(rt);
@@ -205,8 +209,10 @@ void installSequel(jsi::Runtime &rt, const char *docPath)
205209
const jsi::Array &command = batchParams.getValueAtIndex(rt, i).asObject(rt).asArray(rt);
206210
if(command.length(rt) == 0) {
207211
sequel_execute(rt, dbName, "ROLLBACK", jsi::Value::undefined());
208-
jsi::detail::throwJSError(rt, "[react-native-quick-sqlite][execSQLBatch] - No SQL Commands found");
209-
return {};
212+
auto res = jsi::Object(rt);
213+
res.setProperty(rt, "status", jsi::Value(1));
214+
res.setProperty(rt, "message", jsi::String::createFromUtf8(rt, "[react-native-quick-sqlite][execSQLBatch] - No SQL Commands found on batch index " + i));
215+
return move(res);
210216
}
211217
const string query = command.getValueAtIndex(rt, 0).asString(rt).utf8(rt);
212218
const jsi::Value &commandParams = command.length(rt) > 1 ? command.getValueAtIndex(rt, 1) : jsi::Value::undefined();
@@ -252,6 +258,10 @@ void installSequel(jsi::Runtime &rt, const char *docPath)
252258
sequel_execute(rt, dbName, "COMMIT", jsi::Value::undefined());
253259
} catch (...) {
254260
sequel_execute(rt, dbName, "ROLLBACK", jsi::Value::undefined());
261+
auto res = jsi::Object(rt);
262+
res.setProperty(rt, "status", jsi::Value(1));
263+
res.setProperty(rt, "message", jsi::String::createFromUtf8(rt, "[react-native-quick-sqlite][execSQLBatch] - Unexpected error"));
264+
return move(res);
255265
}
256266
auto res = jsi::Object(rt);
257267
res.setProperty(rt, "status", jsi::Value(0));

0 commit comments

Comments
 (0)