Search code examples
rubyruby-on-rails-3facebook-graph-apiexceptionkoala

rescue_from Koala exceptions


Beginner question perhaps:

I'm trying to check my user permissions from facebook with Koala. In some cases I'm going to get thrown an error. So I just want to catch it and redirect to re-authenticate.

  def check_facebook_permissions
    if token = current_user.try(:authentications).find_by_provider('facebook').try(:token)
      graph = Koala::Facebook::API.new(token)
      permissions = graph.get_connections('me','permissions')
      session[:facebook] = {}
      session[:facebook][:ask_publish_actions] = true if permissions[0]['publish_actions'] != true && permissions[0]['publish_stream'] != true
    end
  rescue_from Koala::Facebook::APIError
    # Do something funky here
  end

I thought this was straightforward, but I'm never hitting my rescue. Instead I get:

Koala::Facebook::APIError (OAuthException: Error validating access token: Session has expired at unix time 1324026000. The current unix time is 1324352685.):

What am I missing here?


Solution

  • rescue_from is not a syntactic construct of Ruby like rescue is - it is a normal function, and you need a block to go with it. In your code, no code is given, rescue_from gets executed and effectively skipped - what is after it has no bearing on any exceptions raised before it (just as if you put any other function, like puts, instead of rescue_from).

    See an example of rescue_from use here.

    To make this code work, you need the vanilla Ruby rescue:

    rescue Koala::Facebook::APIError => e