Victus Spiritus

home

Rescuing Ruby Exceptions in Sinatra

10 Mar 2011

This morning granted me 25 minutes to debug an Oauth callback in AutoSub, my latest list subscription juggling tool. While an older spawn of Victus Media ImageBrowser does a fine job of doing the Oauth handshake in Rails, there were a couple of subtle differences in the implementation.

If memory serves the exception handling in our Rails callback is:

def callback
 begin
   *magic code*
 rescue
   render_to :text => !1
 end
end

If an exception is raised it's rendered as text and sent to the browser.

Here's the Sinatra variant I'm working with:

def callback
 begin
   *magic code*
 rescue
   #do something depending on env['sinatra.error']
 end
end

Here I'm passing the environment variable from Rack back to the browser.

Not much of a difference right? But getting a meaningful error message is priceless when trying to understand what's going wrong. Although I'm handling the callback route in AutoSub.rb, and the params are loaded properly from the callback url, there's an issue with one of the method calls I'm making. I should have a few moments this afternoon to clear it up.

update I removed the rescue and allowed the app to raise an error and render the results automatically.

def callback
   *magic code*
end

Looks like I'm running into a token usage error.