diff --git a/whtree/jssdk/whdb/src/impl.ts b/whtree/jssdk/whdb/src/impl.ts index 4494e6b4a6..0f41f308a2 100644 --- a/whtree/jssdk/whdb/src/impl.ts +++ b/whtree/jssdk/whdb/src/impl.ts @@ -170,8 +170,8 @@ class Work implements WorkObject { } catch (e) { try { await this.rollback(); - } catch (ignore) { - //TODO a rollback finish handler might throw but we can't deal with that. Ignore that for now + } catch (rollbackError) { + throw new AggregateError([e, rollbackError], "Work preparation and rollback failed"); } throw e; } diff --git a/whtree/modules/webhare_testsuite/tests/system/nodejs/test_work_prepare_errors.ts b/whtree/modules/webhare_testsuite/tests/system/nodejs/test_work_prepare_errors.ts new file mode 100644 index 0000000000..4f5d3156fd --- /dev/null +++ b/whtree/modules/webhare_testsuite/tests/system/nodejs/test_work_prepare_errors.ts @@ -0,0 +1,26 @@ +import * as test from "@webhare/test"; +import { CodeContext } from "@webhare/services/src/codecontexts"; +import { query, runInWork, onFinishWork, isWorkOpen } from "@webhare/whdb"; + +async function commitPreparationAndRollbackFailuresRemainVisible() { + await using context = new CodeContext("commit preparation and rollback failures"); + const preparation = new Error("Synthetic commit preparation failure"); + const rollback = new Error("Synthetic first rollback preparation failure"); + let rollbackAttempts = 0; + await context.run(async () => { + await test.throws(error => error instanceof AggregateError && error.errors.length === 2 + && error.errors[0] === preparation && error.errors[1] === rollback, + () => runInWork(async () => { + onFinishWork({ + onBeforeCommit: () => { throw preparation; }, + onBeforeRollback: () => { if (++rollbackAttempts === 1) throw rollback; }, + }); + })); + test.assert(rollbackAttempts >= 1, "Rollback preparation was attempted"); + test.eq(false, isWorkOpen(), "The interrupted Work is fully rolled back"); + test.eq(42, (await query<{ value: number }>("SELECT 42 AS value")).rows[0].value, + "The connection remains usable after rollback finishes"); + }); +} + +test.runTests([commitPreparationAndRollbackFailuresRemainVisible]); diff --git a/whtree/modules/webhare_testsuite/tests/system/nodejs/testinfo.xml b/whtree/modules/webhare_testsuite/tests/system/nodejs/testinfo.xml index 24954b9f26..d6d2e4308a 100644 --- a/whtree/modules/webhare_testsuite/tests/system/nodejs/testinfo.xml +++ b/whtree/modules/webhare_testsuite/tests/system/nodejs/testinfo.xml @@ -27,6 +27,7 @@ +