Skip to content

Instantly share code, notes, and snippets.

@amw
Created July 11, 2015 19:52
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 amw/96b791e6c134a419528f to your computer and use it in GitHub Desktop.
Save amw/96b791e6c134a419528f to your computer and use it in GitHub Desktop.
BSON::Binary error when data is non-utf8 compatible string
require "bson"
require "digest/sha2"
d = Digest::SHA256.new
d << "test"
string = d.digest
puts string.encoding
binary = BSON::Binary.new string
puts binary.to_bson
mongoid-errors$ruby binary-encoding.rb
ASCII-8BIT
/usr/local/lib/ruby/gems/2.2.0/gems/bson-3.1.2/lib/bson/binary.rb:137:in `encode': "\x9F" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
from /usr/local/lib/ruby/gems/2.2.0/gems/bson-3.1.2/lib/bson/binary.rb:137:in `block in to_bson'
from /usr/local/lib/ruby/gems/2.2.0/gems/bson-3.1.2/lib/bson/encodable.rb:81:in `encode_binary_data_with_placeholder'
from /usr/local/lib/ruby/gems/2.2.0/gems/bson-3.1.2/lib/bson/binary.rb:134:in `to_bson'
from binary-encoding.rb:9:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment