Skip to content

Commit 9b8b7ec

Browse files
committed
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
1 parent 94fbb24 commit 9b8b7ec

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

Diff for: 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
}

Diff for: packages/pg/test/integration/client/notice-tests.js

+16-9
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,17 +33,17 @@ 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
`
@@ -51,6 +52,12 @@ $$;
5152
})
5253
assert.emits(client, 'notice', function (notice) {
5354
assert.ok(notice != null)
55+
// notice messages should not be error instances
56+
assert(notice instanceof Error === false)
57+
assert.strictEqual(notice.name, 'notice')
58+
assert.strictEqual(notice.message, 'hello, world!')
59+
assert.strictEqual(notice.detail, 'this is a test')
60+
assert.strictEqual(notice.code, '23505')
5461
done()
5562
})
5663
})

0 commit comments

Comments
 (0)