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