Photo-1 Photo-2 Photo-3 Photo-4 Photo-5 Photo-6

@brentmc79

@brentmc79

Full-time web developer. Part-time smart ass.

I'm Brent Collier.

After a year and a half as an engineer on Twitter's Trust & Safety team, I'm looking for my next gig. Contact me if you know of something interesting.

#

UndefinedConversionError can suck it

Posted on 09/04/2014

I have a bit of code in our app that downloads binary files and writes them to Tempfiles. Some testing led to the following exception:

An Encoding::UndefinedConversionError occurred in analyses#create:

  "\x90" from ASCII-8BIT to UTF-8
  app/models/analysis/malware.rb:193:in `get_file'

Line 193 looked basically like this:

tempfile = Tempfile.new('tmp')
begin
  tempfile.write(uploader.file.read)
ensure
  tempfile.close
end

I actually didn't want it converted to UTF-8, so a quick perusal of the Tempfile docs told me that I just needed to specify the encoding when I initialized the Tempfile.

So I changed the first line to:

tempfile = Tempfile.new('tmp', :encoding => 'ascii-8bit')

And everything was kosher.