Skip to content

Commit 066908b

Browse files
committed
libsql-client: Report batch statement index on error
If a batch statement fails, the SQL over HTTP protocol does report back exactly what step failed. However, we also need to propagate that information to the caller.
1 parent e0b105f commit 066908b

File tree

2 files changed

+66
-27
lines changed

2 files changed

+66
-27
lines changed

packages/libsql-client/src/hrana.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,28 @@ export abstract class HranaTransaction implements Transaction {
134134
}
135135

136136
const resultSets = [];
137-
for (const rowsPromise of rowsPromises) {
138-
const rows = await rowsPromise;
139-
if (rows === undefined) {
140-
throw new LibsqlError(
141-
"Statement in a transaction was not executed, " +
142-
"probably because the transaction has been rolled back",
143-
"TRANSACTION_CLOSED",
144-
);
137+
for (let i = 0; i < rowsPromises.length; i++) {
138+
try {
139+
const rows = await rowsPromises[i];
140+
if (rows === undefined) {
141+
throw new LibsqlError(
142+
`Statement at index ${i} in a transaction was not executed, ` +
143+
"probably because the transaction has been rolled back",
144+
"TRANSACTION_CLOSED",
145+
);
146+
}
147+
resultSets.push(resultSetFromHrana(rows));
148+
} catch (e) {
149+
if (e instanceof LibsqlError) {
150+
throw new LibsqlError(
151+
`Statement at index ${i} failed: ${e.message}`,
152+
e.code,
153+
e.rawCode,
154+
e.cause,
155+
);
156+
}
157+
throw e;
145158
}
146-
resultSets.push(resultSetFromHrana(rows));
147159
}
148160
return resultSets;
149161
} catch (e) {
@@ -295,15 +307,27 @@ export async function executeHranaBatch(
295307

296308
const resultSets = [];
297309
await beginPromise;
298-
for (const stmtPromise of stmtPromises) {
299-
const hranaRows = await stmtPromise;
300-
if (hranaRows === undefined) {
301-
throw new LibsqlError(
302-
"Statement in a batch was not executed, probably because the transaction has been rolled back",
303-
"TRANSACTION_CLOSED",
304-
);
310+
for (let i = 0; i < stmtPromises.length; i++) {
311+
try {
312+
const hranaRows = await stmtPromises[i];
313+
if (hranaRows === undefined) {
314+
throw new LibsqlError(
315+
`Statement at index ${i} in a batch was not executed, probably because the transaction has been rolled back`,
316+
"TRANSACTION_CLOSED",
317+
);
318+
}
319+
resultSets.push(resultSetFromHrana(hranaRows));
320+
} catch (e) {
321+
if (e instanceof LibsqlError) {
322+
throw new LibsqlError(
323+
`Statement at index ${i} failed: ${e.message}`,
324+
e.code,
325+
e.rawCode,
326+
e.cause,
327+
);
328+
}
329+
throw e;
305330
}
306-
resultSets.push(resultSetFromHrana(hranaRows));
307331
}
308332
await commitPromise;
309333

packages/libsql-client/src/sqlite3.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,33 @@ export class Sqlite3Client implements Client {
148148
const db = this.#getDb();
149149
try {
150150
executeStmt(db, transactionModeToBegin(mode), this.#intMode);
151-
const resultSets = stmts.map((stmt) => {
152-
if (!db.inTransaction) {
153-
throw new LibsqlError(
154-
"The transaction has been rolled back",
155-
"TRANSACTION_CLOSED",
151+
const resultSets = [];
152+
for (let i = 0; i < stmts.length; i++) {
153+
try {
154+
if (!db.inTransaction) {
155+
throw new LibsqlError(
156+
`Statement at index ${i}: The transaction has been rolled back`,
157+
"TRANSACTION_CLOSED",
158+
);
159+
}
160+
const normalizedStmt: InStatement = Array.isArray(stmts[i])
161+
? { sql: stmts[i][0], args: stmts[i][1] || [] }
162+
: stmts[i];
163+
resultSets.push(
164+
executeStmt(db, normalizedStmt, this.#intMode),
156165
);
166+
} catch (e) {
167+
if (e instanceof LibsqlError) {
168+
throw new LibsqlError(
169+
`Statement at index ${i} failed: ${e.message}`,
170+
e.code,
171+
e.rawCode,
172+
e.cause,
173+
);
174+
}
175+
throw e;
157176
}
158-
const normalizedStmt: InStatement = Array.isArray(stmt)
159-
? { sql: stmt[0], args: stmt[1] || [] }
160-
: stmt;
161-
return executeStmt(db, normalizedStmt, this.#intMode);
162-
});
177+
}
163178
executeStmt(db, "COMMIT", this.#intMode);
164179
return resultSets;
165180
} finally {

0 commit comments

Comments
 (0)