Skip to content

Commit 21b597e

Browse files
author
Homme Zwaagstra
committed
All errors are now instances of the built in Error class
This is a fix for issue brianc#50. It alters both the native binding and the javascript binding to ensure that any errors returned by Postgresql are returned to the client code as instances of the built in Error class. The test code has been updated to assert that this is the case. Rather than run some individual tests the updated test code ensures this is the case for *all* errors encountered during a test run.
1 parent 1731def commit 21b597e

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lib/connection.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,30 @@ p.parseD = function(msg) {
379379
};
380380

381381
//parses error
382-
p.parseE = function(msg) {
382+
p.parseE = function(input) {
383383
var fields = {};
384+
var msg, item;
384385
var fieldType = this.readString(1);
385386
while(fieldType != '\0') {
386387
fields[fieldType] = this.parseCString();
387388
fieldType = this.readString(1);
388389
}
390+
if (input.name === 'error') {
391+
// the msg is an Error instance
392+
msg = new Error(fields.M);
393+
for (item in input) {
394+
// copy input properties to the error
395+
if (input.hasOwnProperty(item)) {
396+
msg[item] = input[item];
397+
}
398+
}
399+
} else {
400+
// the msg is an object literal
401+
msg = input;
402+
msg.message = fields.M;
403+
}
389404
msg.severity = fields.S;
390405
msg.code = fields.C;
391-
msg.message = fields.M;
392406
msg.detail = fields.D;
393407
msg.hint = fields.H;
394408
msg.position = fields.P;

src/binding.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ static Persistent<String> row_symbol;
2121
static Persistent<String> notice_symbol;
2222
static Persistent<String> severity_symbol;
2323
static Persistent<String> code_symbol;
24-
static Persistent<String> message_symbol;
2524
static Persistent<String> detail_symbol;
2625
static Persistent<String> hint_symbol;
2726
static Persistent<String> position_symbol;
@@ -59,7 +58,6 @@ class Connection : public EventEmitter {
5958
row_symbol = NODE_PSYMBOL("_row");
6059
severity_symbol = NODE_PSYMBOL("severity");
6160
code_symbol = NODE_PSYMBOL("code");
62-
message_symbol = NODE_PSYMBOL("message");
6361
detail_symbol = NODE_PSYMBOL("detail");
6462
hint_symbol = NODE_PSYMBOL("hint");
6563
position_symbol = NODE_PSYMBOL("position");
@@ -473,10 +471,12 @@ class Connection : public EventEmitter {
473471
void HandleErrorResult(const PGresult* result)
474472
{
475473
HandleScope scope;
476-
Local<Object> msg = Object::New();
474+
//instantiate the return object as an Error with the summary Postgres message
475+
Local<Object> msg = Local<Object>::Cast(Exception::Error(String::New(PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY))));
476+
477+
//add the other information returned by Postgres to the error object
477478
AttachErrorField(result, msg, severity_symbol, PG_DIAG_SEVERITY);
478479
AttachErrorField(result, msg, code_symbol, PG_DIAG_SQLSTATE);
479-
AttachErrorField(result, msg, message_symbol, PG_DIAG_MESSAGE_PRIMARY);
480480
AttachErrorField(result, msg, detail_symbol, PG_DIAG_MESSAGE_DETAIL);
481481
AttachErrorField(result, msg, hint_symbol, PG_DIAG_MESSAGE_HINT);
482482
AttachErrorField(result, msg, position_symbol, PG_DIAG_STATEMENT_POSITION);

test/test-helper.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ assert.emits = function(item, eventName, callback, message) {
3535
},2000);
3636

3737
item.once(eventName, function() {
38+
if (eventName === 'error') {
39+
// belt and braces test to ensure all error events return an error
40+
assert.ok(arguments[0] instanceof Error,
41+
"Expected error events to throw instances of Error but found: " + sys.inspect(arguments[0]));
42+
}
3843
called = true;
3944
clearTimeout(id);
4045
assert.ok(true);
@@ -105,6 +110,7 @@ assert.throws = function(offender) {
105110
try {
106111
offender();
107112
} catch (e) {
113+
assert.ok(e instanceof Error, "Expected " + offender + " to throw instances of Error");
108114
return;
109115
}
110116
assert.ok(false, "Expected " + offender + " to throw exception");
@@ -117,12 +123,14 @@ assert.length = function(actual, expectedLength) {
117123
var expect = function(callback, timeout) {
118124
var executed = false;
119125
var id = setTimeout(function() {
120-
assert.ok(executed, "Expected execution of funtion to be fired");
126+
assert.ok(executed, "Expected execution of function to be fired");
121127
}, timeout || 2000)
122128

123129
return function(err, queryResult) {
124130
clearTimeout(id);
125-
assert.ok(true);
131+
if (err) {
132+
assert.ok(err instanceof Error, "Expected errors to be instances of Error: " + sys.inspect(err));
133+
}
126134
callback.apply(this, arguments)
127135
}
128136
}

0 commit comments

Comments
 (0)