From b8a5138fc7f0ae2b3110049c7efe94a23022a9f2 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Wed, 3 Jan 2018 14:01:16 +0100 Subject: [PATCH] set_client_encoding: Use error message provided by libpq ... instead of our own, less specific text. This is a better fix for #267 : https://bitbucket.org/ged/ruby-pg/issues/267 --- ext/pg_connection.c | 2 +- spec/pg/connection_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ext/pg_connection.c b/ext/pg_connection.c index 9977503d8..16f3e1009 100644 --- a/ext/pg_connection.c +++ b/ext/pg_connection.c @@ -2978,7 +2978,7 @@ pgconn_set_client_encoding(VALUE self, VALUE str) Check_Type(str, T_STRING); if ( (gvl_PQsetClientEncoding(conn, StringValueCStr(str))) == -1 ) { - rb_raise(rb_ePGerror, "Failed to set client encoding (invalid encoding name: %s or connection error)", StringValueCStr(str)); + rb_raise(rb_ePGerror, "%s", PQerrorMessage(conn)); } #ifdef M17N_SUPPORTED pgconn_set_internal_encoding_index( self ); diff --git a/spec/pg/connection_spec.rb b/spec/pg/connection_spec.rb index 377e77041..6b2b0746f 100755 --- a/spec/pg/connection_spec.rb +++ b/spec/pg/connection_spec.rb @@ -1253,6 +1253,12 @@ expect( escaped.encoding ).to eq( Encoding::ISO8859_1 ) expect( escaped.encode ).to eq( "\"Möhre to\"".encode(Encoding::ISO8859_1) ) end + + it "raises appropriate error if set_client_encoding is called with invalid arguments" do + expect { @conn.set_client_encoding( "invalid" ) }.to raise_error(PG::Error, /invalid value/) + expect { @conn.set_client_encoding( :invalid ) }.to raise_error(TypeError) + expect { @conn.set_client_encoding( nil ) }.to raise_error(TypeError) + end end describe "respect and convert character encoding of input strings" do