简体   繁体   中英

Ruby on Rails - Checking against HTTP errors in controller

Just today I've found my fbgraph implementation has started returning a 400 Bad Request error which is causing an internal server error.

The controller looks like:

def fb
    fbclient = FBGraph::Client.new(:client_id => 'ID', :secret_id => 'SECRET')
    @fbname = fbclient.selection.user('129220333799040').feed.info!['data'][0].from.name
    @fbmessage = fbclient.selection.user('129220333799040').feed.info!['data'][0].message
end

How can I check before calling @fbname in my view that I've received a 200 status?

Thanks.

Update: following Devin M's suggestion, I've switched the above action to

def fb
        fbclient = FBGraph::Client.new(:client_id => 'ID', :secret_id => 'SECRET')
        begin
            @fbname = fbclient.selection.user('129220333799040').feed.info!['data'][0].from.name
            @fbmessage = fbclient.selection.user('129220333799040').feed.info!['data'][0].message
        rescue
            @fbname = "Facebook Account"
            @fbmessage = "Facebook's API is a nightmare"
        end
    end

I think that you should write some tests for this, Its hard to work with Facebooks nightmare of an API. Although if you wanted to catch this error try using that way you can catch the specific error and take some action on it in the rescue portion.

begin
rescue
end

If you want me to take a look at the docs and see what you should catch let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM