开发者

Ruby NameError undefined local variable or method `e

class TwitterProfile < ActiveRecord::Base

  def send_status_update(status_update)
    if publish?
      client = Twitter::Client.new( :oauth_token => authentication.token,
                           :oauth_token_secret => authentication.secret)
      client.update(status_update.to_twitter_string)
    end
  rescue Exception => e
    logger.info "Error publishing to twitter: #{e.to_s}"
  end
end

There is a StatusUpdate model and an observer that reposts them to Twitter in after_create. I sometimes get the following exception:

NameError (undefined local variable or method `e' for #<TwitterProfile:0x00000004e44ab8>):
app/models/twitter_profile.rb:23:in `rescue in send_status_update'
app/models/twitter_profile.rb:18:in `send_status_update'
app/models/status_update_observer.rb:6:in `block in after_create'
app/models/status_update_observer.rb:4:in `after_create'
app/models/workout_observer.rb:5:in `after_update'
app/controllers/frames_controller.rb:76:in `update'
app/controllers/application_controller.rb:24:in `call'
app/controllers/application_controller.rb:24:in `block (2 levels) in <class:ApplicationController>'
app/controllers/appl开发者_运维百科ication_controller.rb:10:in `block in <class:ApplicationController>'

What am I missing here?


I have one thing I know and one that's just a wild guess.

The thing I know is that you don't need to call to_s on an overall #{} expression; that will happen automatically. But it does no harm.

My wild guess is that your test case is not really running the code you have posted. What happens if you change e to f?

I should note that rescuing Exception itself is usually a bad idea. You should rescue RuntimeError or StandardError at the highest, and preferably something more specific. You can get fairly strange errors when rescuing Exception because you interfere with threads and interpreter-level events.


You're missing the 'begin' block of the begin/rescue clause.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜