Skip to content

Commit 7feb7df

Browse files
authored
fix(pg): expose detail and hint on errors from the native client (#3780)
1 parent 17ab6e5 commit 7feb7df

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎packages/pg/lib/native/query.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const errorFieldMap = {
3535
sqlState: 'code',
3636
statementPosition: 'position',
3737
messagePrimary: 'message',
38+
messageDetail: 'detail',
39+
messageHint: 'hint',
3840
context: 'where',
3941
schemaName: 'schema',
4042
tableName: 'table',

‎packages/pg/test/native/native-vs-js-error-tests.js‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,41 @@ const NativeClient = require('../../lib/native')
66
const client = new Client()
77
const nativeClient = new NativeClient()
88

9+
// every field of an error the native client reports must be on the javascript one under the
10+
// same name, and with the same value
11+
const compare = (err, nativeErr) => {
12+
for (const key in nativeErr) {
13+
assert.equal(err[key], nativeErr[key], `Expected err.${key} to equal nativeErr.${key}`)
14+
}
15+
}
16+
17+
const bothFail = (text, cb) => {
18+
client.query(text, (err) => {
19+
nativeClient.query(text, (nativeErr) => {
20+
compare(err, nativeErr)
21+
cb()
22+
})
23+
})
24+
}
25+
926
client.connect()
1027
nativeClient.connect((err) => {
11-
client.query('SELECT alsdkfj', (err) => {
12-
client.end()
13-
14-
nativeClient.query('SELECT lkdasjfasd', (nativeErr) => {
15-
for (const key in nativeErr) {
16-
assert.equal(err[key], nativeErr[key], `Expected err.${key} to equal nativeErr.${key}`)
17-
}
18-
nativeClient.end()
28+
assert(!err)
29+
bothFail('SELECT alsdkfj', () => {
30+
// a duplicate key carries a detail, a misspelt column a hint. A real table rather than a
31+
// temp one, whose schema is named after the connection
32+
const setup =
33+
'DROP TABLE IF EXISTS native_vs_js_dup; CREATE TABLE native_vs_js_dup (id int PRIMARY KEY); INSERT INTO native_vs_js_dup VALUES (1)'
34+
client.query(setup, (err) => {
35+
assert(!err)
36+
bothFail('INSERT INTO native_vs_js_dup VALUES (1)', () => {
37+
bothFail('SELECT cols FROM (SELECT 1 AS col) t', () => {
38+
client.query('DROP TABLE native_vs_js_dup', () => {
39+
client.end()
40+
nativeClient.end()
41+
})
42+
})
43+
})
1944
})
2045
})
2146
})

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo