Intercept every exception in Ruby - ruby

I used exception_notifier gem and Airbrake, but I would like to intercept and send the error, environment and backtrace to a web service. I think I should monkey patch Object::Exception but I am not sure how. I do not want to change the behaviour of Object::Exception but just intercept and send its data. I would like to do it for any Ruby app, so I would like an agnostic solution. If it is not possible a framework-agnostic solution, a Rails solution is better than nothing, but I guess I could study the exception_notification gem.

For Rails, you can put the following in application_controller.rb:
rescue_from Exception do |e|
# do whatever you want with the exception
# and if you still want the exception to continue propagating, then:
raise e
end

Related

Padrino send errors automatically via email

Can anyone suggest a way to have any application errors in a Padrino app send these errors via email?
I already have the Padrino Mailer configured properly and can send test emails, I just have no idea how to configure the app to send me a report via mail (as well as logging it, of course) whenever an error occurs.
Thanks.
I ended up using the padrino-contrib gem. With it, you can install the plugin you need (of course you can do it manually also):
padrino-gen plugin exception_notifier
Which will add it to your gem file and also edit your app/app.rb and boot.rb files to load this gem.
Then in app/app.rb you put something like:
register Padrino::Contrib::ExceptionNotifier
set :exceptions_from, "noreply#domain.com"
set :exceptions_to, "your_address.domain.com"
And that's it.
The nice thing about letting the plugin install it for you is that if you're more familiar with Rails than Padrino (as is my case) this will not only set things up for you, but also show you were the directives need to go.
Hope this helps someone else.
A good approach should be using exception handlers. Adding a begin..rescue block to your code, and if there is an exception, you send the email and continue to the desired behavior.
def some_action
begin
# some code that could go wrong
rescue SomeExceptionClass => some_variable
# here you send the email with the errors
# render stuff, redirect stuff, etc
end
end

How do I get Twitter follow request with the Tweetstream Ruby gem?

According to this Ars Technica tutorial, "the streaming API makes it easy to detect when the user gets a new follower. To detect follow event objects, look for the "event" key and check if it has the string "follow" as the value."
http://arstechnica.com/information-technology/2010/04/tutorial-use-twitters-new-real-time-stream-api-in-python/2/
The tweetstream gem supposedly exposes all methods from the API. However I can't figure out, how to get to those follow requests!
Any ideas?
The tweetstream gem is fairly complete, but sometimes you need to delve into the source to find stuff:
client.on_event(:follow) do |event|
p event[:source][:screen_name]
end
https://github.com/intridea/tweetstream/blob/master/lib/tweetstream/client.rb#L375 :)

RSpec: Mocks, Stubs and Verification

I'm writing a plugin for Cinch (the IRC bot), and trying to write some RSpec tests for it.
However, I'm trying to get to grips with RSpec, and mocking out the external dependencies of this plugin. I want to test two different things for now - that it says hello to people, and that it keeps track of who it's said hello to.
So I've got a method, say_hello, which takes a Cinch::Message. What's the easiest way to mock this particular class? I'm used to Mockito and Java, so I'm used to making a mock of a particular class.
How can I make a mock of Cinch::Message? In my first test, I want to assert that the reply method is called on that message. In the next one, I just want it to respond like a Cinch::Message would, as I only care about the tracking the class does, not the interaction with the message.
I've only just started out with RSpec, so maybe I'm missing something fundamental. Should I be using a mock? A stub?
Thanks!
First of all documentation would be a nice start point, see: https://www.relishapp.com/rspec/rspec-mocks/v/2-11/docs/message-expectations
..and this is how your test could look:
it 'should call #reply on the message'
message = double('cinch message')
message.should_receive(:reply)
object_under_test.say_hello(message)
end

Is there any exception notification gem for ruby project?

I am developing a ruby project, not rails project. Is there any exception notification gem for this case? thanks!
Although quite young, the following is an intentionally non-Rails exception notification gem: https://github.com/Moove-it/rusen
The simpliest solution depends from wich method did you like to send mails.
If you have for example already installed sendmail - in this case to send the mail with notification log you can call it with params. It depend on your system settings.

Stubbing Sinatra helper in Cucumber

I am currently struggling with stubbing out a helper method of my Sinatra app from within Cucumber.
I have a Sinatra app with simple session authentication (by cookies) and I want to turn of authentication by stubbing out the logged_in? helper method for my Cucumber scenarios.
There seems to be a problem with Sinatra and Cucumber concerning sessions so I thought about just using Mocha to work around the problem.
However I don't know how I can access the Sinatra::Application instance from within a Given-Block to stub out the method.
It seems like I need to directly override my Authentication mechanism within a Before do ... end-block
So I ended up with a hooks.rb placed in features/support/ file overwriting my logged_in? and the current_user method.
Before do
MySinatraApplicationClass.class_eval do
helpers do
def logged_in?
return true
end
def current_user
# This returns a certain Username usually stored
# in the session, returning it like
# that prohibits different user logins, but for
# now this is enough for me
"Walter"
end
end
end
end
The only thing I had to take care of, is that the no other actions within the application directly read from session but rather use those helpers.
Sadly I think this way of handling session based Sinatra applications through Cucumber is already described somewhere else and I just thought my problem was different.
You can get the right context by using Sinatra::Application.class_eval
Edit: See original poster's answer for full explanation.

Resources