|
17 | 17 | * so nothing written inside it was stored". Now, when the rollback threw, |
18 | 18 | * the lead says that nothing is known about what is stored. |
19 | 19 | * |
| 20 | + * [#29527] …and that holds for a refusal an earlier conversion already |
| 21 | + * made: a cascade-stopped or hook-stopped refusal escaping the transaction |
| 22 | + * said "under a transaction that rolls back nothing was written". It is |
| 23 | + * re-led too, its `cause` kept. |
| 24 | + * |
20 | 25 | * Controls: a delete of the sub itself still answers `ERR_SUMMARY_RECOMPUTE` |
21 | 26 | * with its own result; a transaction whose rollback succeeds still says it |
22 | | - * rolled back. |
| 27 | + * rolled back, and leaves a converted refusal exactly as it escaped. |
23 | 28 | * |
24 | 29 | * The driver rolls back for real (a snapshot per transaction), refuses every |
25 | 30 | * write to `bucket` while `refuseBucket` is set (the roll-up recompute's parent |
|
29 | 34 |
|
30 | 35 | import { describe, it, expect } from 'vitest'; |
31 | 36 | import { ObjectQL } from './engine.js'; |
32 | | -import { SummaryRecomputeError, cascadeStoppedRefusal, hookStoppedRefusal } from './summary-errors.js'; |
| 37 | +import { |
| 38 | + SummaryRecomputeError, |
| 39 | + cascadeStoppedRefusal, |
| 40 | + hookStoppedRefusal, |
| 41 | + isUnlandedWriteRefusal, |
| 42 | + rollbackFailedRefusal, |
| 43 | + rolledBackRefusal, |
| 44 | +} from './summary-errors.js'; |
33 | 45 |
|
34 | 46 | type Row = Record<string, unknown>; |
35 | 47 |
|
@@ -222,4 +234,58 @@ describe('ObjectQL.transaction never claims "rolled back" when its rollback thre |
222 | 234 | expect(driver.rolledBack).toHaveLength(1); |
223 | 235 | expect(ids(driver.rowsOf('sub'))).toEqual(['s1']); |
224 | 236 | }); |
| 237 | + |
| 238 | + it('[#29527] a cascade-stopped refusal escaping it, the rollback throwing: re-led, the clause about a rollback gone', async () => { |
| 239 | + const { engine, driver } = await makeEngine(); |
| 240 | + driver.state.failRollback = true; |
| 241 | + const err = await refusal(engine.transaction(async (trx: any) => engine.delete('line', { where: { id: 'l1' }, context: trx }))); |
| 242 | + expect(err).toBeInstanceOf(Error); |
| 243 | + expect(err.code).toBe('INTERNAL_ERROR'); |
| 244 | + expect(err.status).toBe(500); |
| 245 | + expect(err.message.startsWith( |
| 246 | + 'The transaction did not complete, and its rollback failed too, so what was written inside it may or may not be stored.', |
| 247 | + ), err.message).toBe(true); |
| 248 | + expect(err.message).not.toContain('nothing was written'); |
| 249 | + expect(err.message).toContain('bucket.sub_count'); |
| 250 | + expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 251 | + expect(err.cause?.failures?.[0]?.childObject).toBe('sub'); |
| 252 | + expect(driver.rolledBack).toHaveLength(0); |
| 253 | + }); |
| 254 | + |
| 255 | + it('[#29527] CONTROL the rollback succeeds: the cascade-stopped refusal escapes exactly as it was made', async () => { |
| 256 | + const { engine, driver } = await makeEngine(); |
| 257 | + const err = await refusal(engine.transaction(async (trx: any) => engine.delete('line', { where: { id: 'l1' }, context: trx }))); |
| 258 | + expect(err.message.startsWith('The delete of "line" did not complete: its cascade stopped'), err.message).toBe(true); |
| 259 | + expect(err.cause?.code).toBe('ERR_SUMMARY_RECOMPUTE'); |
| 260 | + expect(driver.rolledBack).toHaveLength(1); |
| 261 | + }); |
| 262 | + |
| 263 | + it('[#29527] every converted refusal is hedged when the rollback threw, and kept when it ran', () => { |
| 264 | + const original = new SummaryRecomputeError([{ childObject: 'sub', parentObject: 'bucket', parentId: 'b1', field: 'sub_count', error: null }], true); |
| 265 | + for (const converted of [cascadeStoppedRefusal(original, 'line'), hookStoppedRefusal(original, 'afterInsert', 'case', 'cleanup')] as any[]) { |
| 266 | + const failed = rollbackFailedRefusal(converted) as any; |
| 267 | + expect(failed.message.startsWith('The transaction did not complete, and its rollback failed too'), failed.message).toBe(true); |
| 268 | + expect(failed.message).not.toContain('nothing was written'); |
| 269 | + expect(failed.cause).toBe(original); |
| 270 | + expect(failed.code).toBe('INTERNAL_ERROR'); |
| 271 | + expect(failed.status).toBe(500); |
| 272 | + expect(rolledBackRefusal(converted)).toBe(converted); |
| 273 | + } |
| 274 | + const other = new Error('something else'); |
| 275 | + expect(rollbackFailedRefusal(other)).toBe(other); |
| 276 | + }); |
| 277 | +}); |
| 278 | + |
| 279 | +describe('[#29527] isUnlandedWriteRefusal answers the conversion and nothing else', () => { |
| 280 | + it('a conversion, at every depth, is one; the original refusal, a plain error and a look-alike are not', () => { |
| 281 | + const original = new SummaryRecomputeError([{ childObject: 'sub', parentObject: 'bucket', parentId: 'b1', field: 'sub_count', error: null }], true); |
| 282 | + const inner = cascadeStoppedRefusal(original, 'line'); |
| 283 | + for (const e of [inner, cascadeStoppedRefusal(inner, 'acct'), hookStoppedRefusal(inner, 'beforeFind', 'case', 'h'), rollbackFailedRefusal(inner)]) { |
| 284 | + expect(isUnlandedWriteRefusal(e)).toBe(true); |
| 285 | + } |
| 286 | + const lookAlike = Object.assign(new Error((inner as Error).message), { status: 500, code: 'INTERNAL_ERROR', cause: original }); |
| 287 | + for (const e of [original, new Error('x'), lookAlike, { ...(inner as object) }, undefined]) { |
| 288 | + expect(isUnlandedWriteRefusal(e)).toBe(false); |
| 289 | + } |
| 290 | + }); |
225 | 291 | }); |
0 commit comments