Skip to content

Commit 2b3072a

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 b3f0728 commit 2b3072a

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
@@ -602,7 +602,7 @@ Connection.prototype._readValue = function (buffer) {
602602
}
603603

604604
// parses error
605-
Connection.prototype.parseE = function (buffer, length) {
605+
Connection.prototype.parseE = function (buffer, length, isNotice) {
606606
var fields = {}
607607
var fieldType = this.readString(buffer, 1)
608608
while (fieldType !== '\0') {
@@ -611,10 +611,10 @@ Connection.prototype.parseE = function (buffer, length) {
611611
}
612612

613613
// the msg is an Error instance
614-
var msg = new Error(fields.M)
614+
var msg = isNotice ? { message: fields.M } : new Error(fields.M)
615615

616616
// for compatibility with Message
617-
msg.name = 'error'
617+
msg.name = isNotice ? 'notice' : 'error'
618618
msg.length = length
619619

620620
msg.severity = fields.S
@@ -638,7 +638,7 @@ Connection.prototype.parseE = function (buffer, length) {
638638

639639
// same thing, different name
640640
Connection.prototype.parseN = function (buffer, length) {
641-
var msg = this.parseE(buffer, length)
641+
var msg = this.parseE(buffer, length, true)
642642
msg.name = 'notice'
643643
return msg
644644
}

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)