Skip to content

Commit 1b7e9b9

Browse files
committed
Properly throw initial cursor errors
1 parent d919c7f commit 1b7e9b9

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

lib/backend.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ function Backend({
1111
onparameter,
1212
onsuspended,
1313
oncomplete,
14+
onerror,
1415
parsers,
1516
onauth,
1617
onready,
1718
transform,
1819
onnotice,
19-
onnotify,
20-
error
20+
onnotify
2121
}) {
2222
let rows = 0
2323

@@ -126,9 +126,7 @@ function Backend({
126126
function CopyData() { /* No handling needed until implemented */ }
127127

128128
function ErrorResponse(x) {
129-
backend.query
130-
? (backend.error = errors.postgres(parseError(x)))
131-
: error(errors.postgres(parseError(x)))
129+
onerror(errors.postgres(parseError(x)))
132130
}
133131

134132
/* c8 ignore next 3 */
@@ -159,7 +157,7 @@ function Backend({
159157

160158
function Authentication(x) {
161159
const type = x.readInt32BE(5)
162-
type !== 0 && onauth(type, x, error)
160+
type !== 0 && onauth(type, x, onerror)
163161
}
164162

165163
function ParameterStatus(x) {

lib/connection.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function Connection(options = {}) {
5252
onparameter,
5353
onsuspended,
5454
oncomplete,
55+
onerror,
5556
transform,
5657
parsers,
5758
onnotify,
@@ -78,6 +79,14 @@ function Connection(options = {}) {
7879
backend.query.cursor && onsuspended(backend.query.result, true)
7980
}
8081

82+
function onerror(x) {
83+
if (!backend.query)
84+
return error(x)
85+
86+
backend.error = x
87+
backend.query.cursor && socket.write(frontend.Sync)
88+
}
89+
8190
function onparse() {
8291
if (backend.query && backend.query.statement.sig)
8392
statements[backend.query.statement.sig] = backend.query.statement

lib/frontend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { errors } = require('./errors.js')
55

66
const N = String.fromCharCode(0)
77
const empty = Buffer.alloc(0)
8+
const Sync = bytes.S().end()
89
const execute = Buffer.concat([
910
bytes.D().str('P').str(N).end(),
1011
bytes.E().str(N).i32(0).end(),
@@ -40,6 +41,7 @@ module.exports = {
4041
SSLRequest,
4142
auth,
4243
Bind,
44+
Sync,
4345
Parse,
4446
Query,
4547
Close,

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function Postgres(a, b) {
285285
}
286286

287287
function unsafe(xs, args, queryOptions) {
288-
const prepare = queryOptions && queryOptions.prepare || false;
288+
const prepare = queryOptions && queryOptions.prepare || false
289289
return query({ raw: true, simple: !args, prepare }, connection || getConnection(), xs, args || [])
290290
}
291291

tests/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,11 @@ t('Cursor throw works', async() => {
991991
return ['1aerr', order.join('')]
992992
})
993993

994+
t('Cursor throw works', async() => [
995+
'err',
996+
await sql`wat`.cursor(() => { /* noop */ }).catch(() => 'err')
997+
])
998+
994999
t('Transform row', async() => {
9951000
const sql = postgres({
9961001
...options,
@@ -1240,13 +1245,13 @@ t('prepare: true enables prepared transactions', async() => {
12401245

12411246
t('prepares unsafe query when "prepare" option is true', async() => {
12421247
const sql = postgres({ ...options, prepare: true })
1243-
const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ["bla"], { prepare: true })
1248+
const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'], { prepare: true })
12441249
return [result[0].statement, 'select * from pg_prepared_statements where name <> $1']
12451250
})
12461251

12471252
t('does not prepare unsafe query by default', async() => {
1248-
const sql = postgres({ ...options, prepare: true });
1249-
const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ["bla"])
1253+
const sql = postgres({ ...options, prepare: true })
1254+
const result = await sql.unsafe('select * from pg_prepared_statements where name <> $1', ['bla'])
12501255
return [0, result.count]
12511256
})
12521257

0 commit comments

Comments
 (0)