Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/drizzle/src/transactions/beginTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const beginTransaction: BeginTransaction = async function beginTransactio
let transaction: DrizzleTransaction

let transactionReady: () => void
let transactionFailed: (err: unknown) => void

// Await initialization here
// Prevent race conditions where the adapter may be
Expand Down Expand Up @@ -44,13 +45,17 @@ export const beginTransaction: BeginTransaction = async function beginTransactio
transactionReady()
})
}, options || this.transactionOptions)
.catch(() => {
// swallow
.catch((err) => {
// Connection failed before callback ran - reject instead of hanging forever
transactionFailed(err)
})

// Need to wait until the transaction is ready
// before binding its `resolve` and `reject` methods below
await new Promise<void>((resolve) => (transactionReady = resolve))
await new Promise<void>((res, rej) => {
transactionReady = res
transactionFailed = rej
})

this.sessions[id] = {
db: transaction,
Expand Down
40 changes: 40 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,46 @@
})
}

it(
Comment thread
AlessioGr marked this conversation as resolved.
'should throw error when beginTransaction fails to connect (drizzle)',
{ db: 'drizzle' },
async () => {
const db = payload.db as unknown as Record<string, unknown>
const originalDrizzle = db.drizzle
try {
db.drizzle = {
transaction: () => Promise.reject(new Error('connection refused')),
}

await expect(() => payload.db.beginTransaction()).rejects.toThrow(/connection refused/)

Check failure on line 2181 in test/database/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite-uuid (3/3)

[int] test/database/int.spec.ts > database > transactions > local api > should throw error when beginTransaction fails to connect (drizzle)

AssertionError: promise resolved "null" instead of rejecting - Expected: Error { "message": "rejected promise", } + Received: null ❯ test/database/int.spec.ts:2181:61

Check failure on line 2181 in test/database/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-sqlite (3/3)

[int] test/database/int.spec.ts > database > transactions > local api > should throw error when beginTransaction fails to connect (drizzle)

AssertionError: promise resolved "null" instead of rejecting - Expected: Error { "message": "rejected promise", } + Received: null ❯ test/database/int.spec.ts:2181:61
} finally {
db.drizzle = originalDrizzle
}
},
)

it(
'should throw error when beginTransaction fails to connect (mongo)',
Comment thread
AlessioGr marked this conversation as resolved.
{ db: 'mongo' },
async () => {
const db = payload.db as unknown as Record<string, unknown>
const originalConnection = db.connection
try {
db.connection = {
getClient: () => ({
startSession: () => {
throw new Error('connection refused')
},
}),
}

await expect(() => payload.db.beginTransaction()).rejects.toThrow(/connection refused/)

Check failure on line 2203 in test/database/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-cosmosdb (3/3)

[int] test/database/int.spec.ts > database > transactions > local api > should throw error when beginTransaction fails to connect (mongo)

AssertionError: promise resolved "null" instead of rejecting - Expected: Error { "message": "rejected promise", } + Received: null ❯ test/database/int.spec.ts:2203:61

Check failure on line 2203 in test/database/int.spec.ts

View workflow job for this annotation

GitHub Actions / int-firestore (3/3)

[int] test/database/int.spec.ts > database > transactions > local api > should throw error when beginTransaction fails to connect (mongo)

AssertionError: promise resolved "null" instead of rejecting - Expected: Error { "message": "rejected promise", } + Received: null ❯ test/database/int.spec.ts:2203:61
} finally {
db.connection = originalConnection
}
},
)

describe('disableTransaction', () => {
let disabledTransactionPost
beforeAll(async () => {
Expand Down
Loading