Skip to content

Commit

Permalink
set_client_encoding: Use error message provided by libpq
Browse files Browse the repository at this point in the history
... instead of our own, less specific text.

This is a better fix for #267 : https://bitbucket.org/ged/ruby-pg/issues/267
  • Loading branch information
larskanis committed Jan 3, 2018
1 parent 15f0e1e commit b8a5138
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/pg_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b8a5138

Please sign in to comment.