Skip to content

Commit 99ddae4

Browse files
committed
Fix errors thrown on commit
1 parent 7e418cd commit 99ddae4

File tree

6 files changed

+69
-12
lines changed

6 files changed

+69
-12
lines changed

cjs/src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,17 @@ function Postgres(a, b) {
210210
const sql = Sql(handler)
211211
sql.savepoint = savepoint
212212
let uncaughtError
213+
, result
214+
213215
name && await sql`savepoint ${ sql(name) }`
214216
try {
215-
const result = await new Promise((resolve, reject) => {
217+
result = await new Promise((resolve, reject) => {
216218
const x = fn(sql)
217219
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
218220
})
219221

220222
if (uncaughtError)
221223
throw uncaughtError
222-
223-
!name && await sql`commit`
224-
return result
225224
} catch (e) {
226225
await (name
227226
? sql`rollback to ${ sql(name) }`
@@ -230,6 +229,9 @@ function Postgres(a, b) {
230229
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
231230
}
232231

232+
!name && await sql`commit`
233+
return result
234+
233235
function savepoint(name, fn) {
234236
if (name && Array.isArray(name.raw))
235237
return savepoint(sql => sql.apply(sql, arguments))

cjs/tests/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,3 +2123,20 @@ t('Supports arrays of fragments', async() => {
21232123
x
21242124
]
21252125
})
2126+
2127+
t('Does not try rollback when commit errors', async() => {
2128+
let notice = null
2129+
const sql = postgres({ ...options, onnotice: x => notice = x })
2130+
await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`
2131+
2132+
await sql.begin('isolation level serializable', async sql => {
2133+
await sql`insert into test values(1)`
2134+
await sql`insert into test values(1)`
2135+
}).catch(e => e)
2136+
2137+
return [
2138+
notice,
2139+
null,
2140+
await sql`drop table test`
2141+
]
2142+
})

deno/src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,17 @@ function Postgres(a, b) {
211211
const sql = Sql(handler)
212212
sql.savepoint = savepoint
213213
let uncaughtError
214+
, result
215+
214216
name && await sql`savepoint ${ sql(name) }`
215217
try {
216-
const result = await new Promise((resolve, reject) => {
218+
result = await new Promise((resolve, reject) => {
217219
const x = fn(sql)
218220
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
219221
})
220222

221223
if (uncaughtError)
222224
throw uncaughtError
223-
224-
!name && await sql`commit`
225-
return result
226225
} catch (e) {
227226
await (name
228227
? sql`rollback to ${ sql(name) }`
@@ -231,6 +230,9 @@ function Postgres(a, b) {
231230
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
232231
}
233232

233+
!name && await sql`commit`
234+
return result
235+
234236
function savepoint(name, fn) {
235237
if (name && Array.isArray(name.raw))
236238
return savepoint(sql => sql.apply(sql, arguments))

deno/tests/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,4 +2126,21 @@ t('Supports arrays of fragments', async() => {
21262126
]
21272127
})
21282128

2129+
t('Does not try rollback when commit errors', async() => {
2130+
let notice = null
2131+
const sql = postgres({ ...options, onnotice: x => notice = x })
2132+
await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`
2133+
2134+
await sql.begin('isolation level serializable', async sql => {
2135+
await sql`insert into test values(1)`
2136+
await sql`insert into test values(1)`
2137+
}).catch(e => e)
2138+
2139+
return [
2140+
notice,
2141+
null,
2142+
await sql`drop table test`
2143+
]
2144+
})
2145+
21292146
;window.addEventListener("unload", () => Deno.exit(process.exitCode))

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,17 @@ function Postgres(a, b) {
210210
const sql = Sql(handler)
211211
sql.savepoint = savepoint
212212
let uncaughtError
213+
, result
214+
213215
name && await sql`savepoint ${ sql(name) }`
214216
try {
215-
const result = await new Promise((resolve, reject) => {
217+
result = await new Promise((resolve, reject) => {
216218
const x = fn(sql)
217219
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
218220
})
219221

220222
if (uncaughtError)
221223
throw uncaughtError
222-
223-
!name && await sql`commit`
224-
return result
225224
} catch (e) {
226225
await (name
227226
? sql`rollback to ${ sql(name) }`
@@ -230,6 +229,9 @@ function Postgres(a, b) {
230229
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
231230
}
232231

232+
!name && await sql`commit`
233+
return result
234+
233235
function savepoint(name, fn) {
234236
if (name && Array.isArray(name.raw))
235237
return savepoint(sql => sql.apply(sql, arguments))

tests/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,3 +2123,20 @@ t('Supports arrays of fragments', async() => {
21232123
x
21242124
]
21252125
})
2126+
2127+
t('Does not try rollback when commit errors', async() => {
2128+
let notice = null
2129+
const sql = postgres({ ...options, onnotice: x => notice = x })
2130+
await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`
2131+
2132+
await sql.begin('isolation level serializable', async sql => {
2133+
await sql`insert into test values(1)`
2134+
await sql`insert into test values(1)`
2135+
}).catch(e => e)
2136+
2137+
return [
2138+
notice,
2139+
null,
2140+
await sql`drop table test`
2141+
]
2142+
})

0 commit comments

Comments
 (0)