Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/pg/lib/native/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const errorFieldMap = {
sqlState: 'code',
statementPosition: 'position',
messagePrimary: 'message',
messageDetail: 'detail',
messageHint: 'hint',
context: 'where',
schemaName: 'schema',
tableName: 'table',
Expand Down
41 changes: 33 additions & 8 deletions packages/pg/test/native/native-vs-js-error-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,41 @@ const NativeClient = require('../../lib/native')
const client = new Client()
const nativeClient = new NativeClient()

// every field of an error the native client reports must be on the javascript one under the
// same name, and with the same value
const compare = (err, nativeErr) => {
for (const key in nativeErr) {
assert.equal(err[key], nativeErr[key], `Expected err.${key} to equal nativeErr.${key}`)
}
}

const bothFail = (text, cb) => {
client.query(text, (err) => {
nativeClient.query(text, (nativeErr) => {
compare(err, nativeErr)
cb()
})
})
}

client.connect()
nativeClient.connect((err) => {
client.query('SELECT alsdkfj', (err) => {
client.end()

nativeClient.query('SELECT lkdasjfasd', (nativeErr) => {
for (const key in nativeErr) {
assert.equal(err[key], nativeErr[key], `Expected err.${key} to equal nativeErr.${key}`)
}
nativeClient.end()
assert(!err)
bothFail('SELECT alsdkfj', () => {
// a duplicate key carries a detail, a misspelt column a hint. A real table rather than a
// temp one, whose schema is named after the connection
const setup =
'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)'
client.query(setup, (err) => {
assert(!err)
bothFail('INSERT INTO native_vs_js_dup VALUES (1)', () => {
bothFail('SELECT cols FROM (SELECT 1 AS col) t', () => {
client.query('DROP TABLE native_vs_js_dup', () => {
client.end()
nativeClient.end()
})
})
})
})
})
})
Loading