Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 474
TLK Avatar asked Dec 07 '25 03:12

TLK


1 Answers

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
like image 199
Amadan Avatar answered Dec 08 '25 16:12

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!