Skip to content

Commit 1d48051

Browse files
authored
Make notice messages not an instance of Error (#2090)
* Make notice messages not an instance of Error Slight API cleanup to make a notice instance the same shape as it was, but not be an instance of error. This is a backwards incompatible change though I expect the impact to be minimal. Closes #1982 * skip notice test in travis * Pin [email protected] for regression in async iterators * Check and see if node 13.8 is still borked on async iterator * Yeah, node still has changed edge case behavior on stream * Emit notice messages on travis
1 parent 94fbb24 commit 1d48051

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
node_js:
1111
- lts/dubnium
1212
- lts/erbium
13-
- 13
13+
- 13.6
1414

1515
addons:
1616
postgresql: "10"

packages/pg/lib/connection.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ Connection.prototype._readValue = function (buffer) {
597597
}
598598

599599
// parses error
600-
Connection.prototype.parseE = function (buffer, length) {
600+
Connection.prototype.parseE = function (buffer, length, isNotice) {
601601
var fields = {}
602602
var fieldType = this.readString(buffer, 1)
603603
while (fieldType !== '\0') {
@@ -606,10 +606,10 @@ Connection.prototype.parseE = function (buffer, length) {
606606
}
607607

608608
// the msg is an Error instance
609-
var msg = new Error(fields.M)
609+
var msg = isNotice ? { message: fields.M } : new Error(fields.M)
610610

611611
// for compatibility with Message
612-
msg.name = 'error'
612+
msg.name = isNotice ? 'notice' : 'error'
613613
msg.length = length
614614

615615
msg.severity = fields.S
@@ -633,7 +633,7 @@ Connection.prototype.parseE = function (buffer, length) {
633633

634634
// same thing, different name
635635
Connection.prototype.parseN = function (buffer, length) {
636-
var msg = this.parseE(buffer, length)
636+
var msg = this.parseE(buffer, length, true)
637637
msg.name = 'notice'
638638
return msg
639639
}

packages/pg/test/integration/client/notice-tests.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
2-
var helper = require('./test-helper')
2+
const helper = require('./test-helper')
3+
const assert = require('assert')
34
const suite = new helper.Suite()
45

56
suite.test('emits notify message', function (done) {
6-
var client = helper.client()
7+
const client = helper.client()
78
client.query('LISTEN boom', assert.calls(function () {
8-
var otherClient = helper.client()
9-
var bothEmitted = -1
9+
const otherClient = helper.client()
10+
let bothEmitted = -1
1011
otherClient.query('LISTEN boom', assert.calls(function () {
1112
assert.emits(client, 'notification', function (msg) {
1213
// make sure PQfreemem doesn't invalidate string pointers
@@ -32,25 +33,34 @@ suite.test('emits notify message', function (done) {
3233
})
3334

3435
// this test fails on travis due to their config
35-
suite.test('emits notice message', false, function (done) {
36+
suite.test('emits notice message', function (done) {
3637
if (helper.args.native) {
37-
console.error('need to get notice message working on native')
38+
console.error('notice messages do not work curreintly with node-libpq')
3839
return done()
3940
}
40-
// TODO this doesn't work on all versions of postgres
41-
var client = helper.client()
41+
42+
const client = helper.client()
4243
const text = `
4344
DO language plpgsql $$
4445
BEGIN
45-
RAISE NOTICE 'hello, world!';
46+
RAISE NOTICE 'hello, world!' USING ERRCODE = '23505', DETAIL = 'this is a test';
4647
END
4748
$$;
4849
`
49-
client.query(text, () => {
50-
client.end()
50+
client.query('SET SESSION client_min_messages=notice', (err) => {
51+
assert.ifError(err)
52+
client.query(text, () => {
53+
client.end()
54+
})
5155
})
5256
assert.emits(client, 'notice', function (notice) {
5357
assert.ok(notice != null)
58+
// notice messages should not be error instances
59+
assert(notice instanceof Error === false)
60+
assert.strictEqual(notice.name, 'notice')
61+
assert.strictEqual(notice.message, 'hello, world!')
62+
assert.strictEqual(notice.detail, 'this is a test')
63+
assert.strictEqual(notice.code, '23505')
5464
done()
5565
})
5666
})

0 commit comments

Comments
 (0)