开发者

Ruby on Rails - Checking against HTTP errors in controller

开发者 https://www.devze.com 2023-03-10 03:31 出处:网络
Just today I\'ve found my fbgraph implementation has started returning a 400 Bad Request error which is causing an internal server error.

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

开发者_StackOverflow
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号