native connection failures gracefully emit error from libpq
This commit is contained in:
parent
e1d36359c1
commit
2836c8b64d
@ -103,7 +103,10 @@ public:
|
||||
}
|
||||
|
||||
String::Utf8Value conninfo(args[0]->ToString());
|
||||
self->Connect(*conninfo);
|
||||
bool success = self->Connect(*conninfo);
|
||||
if(!success) {
|
||||
self -> AbortConnection();
|
||||
}
|
||||
|
||||
return Undefined();
|
||||
}
|
||||
@ -279,6 +282,22 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
//aborts connection and returns connection error message
|
||||
char* AbortConnection()
|
||||
{
|
||||
EmitLastError();
|
||||
DestroyConnection();
|
||||
}
|
||||
|
||||
//safely destroys the connection at most 1 time
|
||||
void DestroyConnection()
|
||||
{
|
||||
if(connection_ != NULL) {
|
||||
PQfinish(connection_);
|
||||
connection_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//initializes initial async connection to postgres via libpq
|
||||
//and hands off control to libev
|
||||
bool Connect(const char* conninfo)
|
||||
@ -287,22 +306,18 @@ protected:
|
||||
|
||||
if (!connection_) {
|
||||
LOG("Connection couldn't be created");
|
||||
} else {
|
||||
TRACE("Native connection created");
|
||||
}
|
||||
|
||||
if (PQsetnonblocking(connection_, 1) == -1) {
|
||||
LOG("Unable to set connection to non-blocking");
|
||||
PQfinish(connection_);
|
||||
connection_ = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
ConnStatusType status = PQstatus(connection_);
|
||||
|
||||
if(CONNECTION_BAD == status) {
|
||||
PQfinish(connection_);
|
||||
LOG("Bad connection status");
|
||||
connection_ = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
int fd = PQsocket(connection_);
|
||||
@ -477,7 +492,7 @@ protected:
|
||||
{
|
||||
StopRead();
|
||||
StopWrite();
|
||||
PQfinish(connection_);
|
||||
DestroyConnection();
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -3,11 +3,12 @@ var Client = require(__dirname + "/../../lib/native").Client;
|
||||
|
||||
test('connecting with wrong parameters', function() {
|
||||
var con = new Client("user=asldfkj hostaddr=127.0.0.1 port=5432 dbname=asldkfj");
|
||||
con.connect();
|
||||
assert.emits(con, 'error', function(error) {
|
||||
assert.ok(error != null, "error should not be null");
|
||||
con.end();
|
||||
});
|
||||
|
||||
con.connect();
|
||||
});
|
||||
|
||||
test('connects', function() {
|
||||
|
Loading…
Reference in New Issue
Block a user