Commits

Lars Kanis committed b09cf9d

Add new error class 'ConnectionBad' that is raised if the connection

From 9d0acad61a693b9042842883fa8195ba973bf813 Mon Sep 17 00:00:00 2001
could not be established.
Use 'UnableToSend' error in case that sending fails with async methods.

  • Participants
  • Parent commits 8b9b335

Comments (0)

Files changed (4)

 
 extern VALUE rb_mPG;
 extern VALUE rb_ePGerror;
+extern VALUE rb_eServerError;
+extern VALUE rb_eUnableToSend;
+extern VALUE rb_eConnectionBad;
 extern VALUE rb_mPGconstants;
 extern VALUE rb_cPGconn;
 extern VALUE rb_cPGresult;

ext/pg_connection.c

 	PGconn *conn = pgconn_check( self );
 
 	if ( !conn )
-		rb_raise( rb_ePGerror, "connection is closed" );
+		rb_raise( rb_eConnectionBad, "connection is closed" );
 
 	return conn;
 }
 	DATA_PTR(self) = conn;
 
 	if (PQstatus(conn) == CONNECTION_BAD) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eConnectionBad, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", self);
 		rb_exc_raise(error);
 	}
 	DATA_PTR(rb_conn) = conn;
 
 	if ( PQstatus(conn) == CONNECTION_BAD ) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eConnectionBad, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", rb_conn);
 		rb_exc_raise(error);
 	}
 	/* If called with no parameters, use PQsendQuery */
 	if(NIL_P(params)) {
 		if(PQsendQuery(conn,StringValuePtr(command)) == 0) {
-			error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+			error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
 			rb_iv_set(error, "@connection", self);
 			rb_exc_raise(error);
 		}
 	xfree(paramFormats);
 
 	if(result == 0) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", self);
 		rb_exc_raise(error);
 	}
 	xfree(paramTypes);
 
 	if(result == 0) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", self);
 		rb_exc_raise(error);
 	}
 	xfree(paramFormats);
 
 	if(result == 0) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", self);
 		rb_exc_raise(error);
 	}
 	PGconn *conn = pg_get_pgconn(self);
 	/* returns 0 on failure */
 	if(PQsendDescribePrepared(conn,StringValuePtr(stmt_name)) == 0) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", self);
 		rb_exc_raise(error);
 	}
 	PGconn *conn = pg_get_pgconn(self);
 	/* returns 0 on failure */
 	if(PQsendDescribePortal(conn,StringValuePtr(portal)) == 0) {
-		error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
+		error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
 		rb_iv_set(error, "@connection", self);
 		rb_exc_raise(error);
 	}
 VALUE rb_ePGerror;
 VALUE rb_eServerError;
 VALUE rb_eUnableToSend;
+VALUE rb_eConnectionBad;
 
 static VALUE
 define_error_class(const char *name, const char *baseclass_code)
 
 	rb_eServerError = rb_define_class_under( rb_mPG, "ServerError", rb_ePGerror );
 	rb_eUnableToSend = rb_define_class_under( rb_mPG, "UnableToSend", rb_ePGerror );
+	rb_eConnectionBad = rb_define_class_under( rb_mPG, "ConnectionBad", rb_ePGerror );
 
 	#include "errorcodes.def"
 }

spec/pg/connection_spec.rb

 		conn = PG.connect( @conninfo )
 
 		conn.finish
-		expect { conn.finish }.to raise_error( PG::Error, /connection is closed/i )
+		expect { conn.finish }.to raise_error( PG::ConnectionBad, /connection is closed/i )
 	end
 
 	it "closes the IO fetched from #socket_io when the connection is closed", :without_transaction, :socket_io do
 		io = conn.socket_io
 		conn.finish
 		io.should be_closed()
-		expect { conn.socket_io }.to raise_error( PG::Error, /connection is closed/i )
+		expect { conn.socket_io }.to raise_error( PG::ConnectionBad, /connection is closed/i )
 	end
 
 	it "closes the IO fetched from #socket_io when the connection is reset", :without_transaction, :socket_io do
 				serv = TCPServer.new( '127.0.0.1', 54320 )
 				expect {
 					described_class.new( '127.0.0.1', 54320, "", "", "me", "xxxx", "somedb" )
-				}.to raise_error(PG::Error, /server closed the connection unexpectedly/)
+				}.to raise_error(PG::ConnectionBad, /server closed the connection unexpectedly/)
 			end
 
 			sleep 0.5