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 redirect them to Twitter in after_create . Sometimes I 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/application_controller.rb:10:in `block in <class:ApplicationController>' 

What am I missing here?

+4
source share
2 answers

I have one thing that I know, and one that is just a wild hunch.

I know that you do not need to call to_s in the general expression #{} ; this will happen automatically. But it does not harm.

My wild guess is that your test case doesn’t actually run the code you posted. What happens if you change e to f ?

I must point out that saving the Exception itself is usually bad. You should save RuntimeError or StandardError at the highest, and preferably something more specific. You can get some rather strange errors when saving exceptions because you interfere with threads and events at the interpreter level.

+2
source

You are missing the "begin" block in the begin / rescue clause.

-1
source

Source: https://habr.com/ru/post/1369399/


All Articles