11
12
09

Hpricot – [BUG] Bus Error – Solution / Workaround

Problem:

You’re using Hpricot to parse web content, but it’s throwing an error like this that completely kills the process (probably crashing your app, or your background task, as the case may be):

/usr/local/lib/ruby/gems/1.8/gems/hpricot-0.8.2/lib/hpricot/parse.rb:33: [BUG] Bus Error
ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-darwin8.11.1]
Abort trap

This resource suggests that the problem is that the content retrieved is precisely 16384 bytes long, however, that was not the problem in my case.

My problem is replicated in this gist. Examination of the URL it was trying to retrieve using curl with -i indicated that this was returning a 302 redirect:

HTTP/1.1 302 Found
Date: Thu, 12 Nov 2009 14:50:53 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
X-UA-Compatible: IE=EmulateIE7
Location: /
Set-Cookie: ASP.NET_SessionId=p2s0dljru11tiwer3e01jfq2; path=/; HttpOnly
Set-Cookie: Forum2backURL=/tm.aspx?m=1859288#1859354; path=/
Set-Cookie: Forum2preURL=; path=/
Cache-Control: private
Expires: Wed, 11 Nov 2009 13:50:53 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 120

I am not sure why Ruby’s OpenURI open method was not capable of parsing / following this redirect. However, I determined that the file returned by open() had a size of zero bytes, and this was causing Hpricot to blow up.

My workaround is just to check the size of the file returned by open() and only try to parse it if it is greater than 0:

f = open(file_or_uri)
if f.size > 0
doc = Hpricot(f)
else
raise "Could not retrieve content due to zero-sized file, possibly due to site redirect."
end

07
17
08

Fixing HAML in jEdit

(This one is purely for tech people, Ruby on Rails developers who use jEdit to be exact.)

Chances are, if you use jEdit and HAML, you’re using or you’ve tried out Jim MorrisjEdit HAML edit mode.

If you’ve done so, and you’re using an up-to-date version of jEdit (e.g. 4.3 pre14) you’ll have noticed that the way it deals with indentation is really buggy. Here’s a description of the problem I posted to the jEdit mailing list, without much luck:

This is the code I want:

%table.form
  = form_row("First name")

In the code above, as soon as I type the “a” in name (actually the point varies a bit, could be anywhere inside the double quotes), the line unindents, giving me this:

%table.form
= form_row("First na

This means that I have to go and reindent the line, so that it’s back the way I want it to be.

This also happens in cases where I am using braces { }. Here is the code I may want:

%a{:href => "/some/link"}
  = "#{@user.login}"

As soon as I type the opening brace { in that string, the line unindents:

%a{:href => "/some/link"}
= "#{

And again, I have to reindent it.

What’s even more annoying (incredibly annoying!) is that after I reindent it, as I continue to type, it will continually unindent. So I end up fighting with my editor.

You can fix this problem by hacking up a new Ruby mode for jEdit that removes all of the indentation rules. Then, change the HAML edit mode to delegate to your new hacked Ruby mode, instead of the proper one.

Step 1:

Open the jEdit catalog file, probably in the modes folder (precise location depends on your system). Find the entry for Ruby and create a new one called ruby_stripped_down that references a new file called ruby_stripped_down.xml:

<mode NAME="ruby_stripped_down" FILE="ruby_stripped_down.xml" />

You don’t need all the info about file extensions, since this mode will only get called from the HAML mode.

Step 2:

Open up the ruby.xml file from the same folder as the catalog. Save a copy of it as ruby_stripped_down.xml. Now delete all of the lines inside the <props> tags near the beginning.

Step 3:

Open up the haml.xml file from the same folder. Wherever you see:

DELEGATE="ruby::MAIN"

Change it to:

DELEGATE="ruby_stripped_down::MAIN"

This occurs in four places.

Save it. Restart jEdit. Your problem should be solved.

If you’re lazy, you can also download my edited haml, ruby_stripped_down, and catalog files. However, be warned that overwriting your versions is at your risk (especially the catalog file – the others aren’t nearly as risky).

haml.xml
ruby_stripped_down.xml
catalog



Life, politics, code and current events from a Canadian perspective.

Adrian Duyzer
Email me

twitter.com/adriandz

Proud contributor to
Director, Web Division at

Feeds

Meta