Skip to content

Instantly share code, notes, and snippets.

@robheittman
Created January 1, 2012 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robheittman/1548042 to your computer and use it in GitHub Desktop.
Save robheittman/1548042 to your computer and use it in GitHub Desktop.
Demonstrate an issue with encoding of error messages
Created database encoding_test_1325444057
Error message encoding is: ASCII-8BIT
#<Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT>
pg_issue.rb:17:in `rescue in <main>'
pg_issue.rb:7:in `<main>'
Dropped database encoding_test_1325444057
# encoding: UTF-8
require 'pg'
dbname = "encoding_test_#{Time.now.to_i}"
`createdb -E UTF-8 #{dbname}`
puts "Created database #{dbname}"
begin
conn = PGconn.connect "localhost", 5432, "", "", dbname
conn.exec "create table foo (bar text)"
# This command produces a syntax error
sql = "insert into foo values ('Côte d'Ivoire')"
conn.exec sql
rescue => e
# The error message from PG is not in UTF-8, but ASCII-8BIT
puts "Error message encoding is: #{e.message.encoding.name}"
begin
puts "The query was #{sql}; the error message is: #{e.message}"
rescue => e2
puts e2.inspect
puts e2.backtrace
end
end
conn.close
`dropdb #{dbname}`
puts "Dropped database #{dbname}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment