From 2a48caa341716babb04caba9028ec8bf60021c5a Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 12 Jul 2021 15:54:25 +0200 Subject: [PATCH] improve error messages in DB transactions --- packages/taler-wallet-core/src/util/query.ts | 21 +++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/taler-wallet-core/src/util/query.ts b/packages/taler-wallet-core/src/util/query.ts index c7c08b47c..b76bf6b6b 100644 --- a/packages/taler-wallet-core/src/util/query.ts +++ b/packages/taler-wallet-core/src/util/query.ts @@ -87,6 +87,15 @@ interface CursorValueResult { value: T; } +class TransactionAbortedError extends Error { + constructor(m: string) { + super(m); + + // Set the prototype explicitly. + Object.setPrototypeOf(this, TransactionAbortedError.prototype); + } +} + class ResultStream { private currentPromise: Promise; private gotCursorEnd = false; @@ -396,6 +405,7 @@ function runTx( return new Promise((resolve, reject) => { let funResult: any = undefined; let gotFunResult = false; + let transactionException: any = undefined; tx.oncomplete = () => { // This is a fatal error: The transaction completed *before* // the transaction function returned. Likely, the transaction @@ -416,12 +426,16 @@ function runTx( logger.error(`${stack}`); }; tx.onabort = () => { + let msg: string; if (tx.error) { - logger.error("Transaction aborted with error:", tx.error); + msg = `Transaction aborted (transaction error): ${tx.error}`; + } else if (transactionException !== undefined) { + msg = `Transaction aborted (exception thrown): ${transactionException}`; } else { - logger.error("Transaction aborted (no error)"); + msg = "Transaction aborted (no DB error)"; } - reject(TransactionAbort); + logger.error(msg); + reject(new TransactionAbortedError(msg)); }; const resP = Promise.resolve().then(() => f(arg)); resP @@ -433,6 +447,7 @@ function runTx( if (e == TransactionAbort) { logger.trace("aborting transaction"); } else { + transactionException = e; console.error("Transaction failed:", e); console.error(stack); tx.abort();