Skip to content

Commit

Permalink
Capturing exceptions raised in EventListener callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
afcapel committed Sep 1, 2011
1 parent d001a5a commit f7a5650
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -52,7 +52,7 @@ events, such as when a client subscribe to a channel.

```ruby
# A chat listener will by default listen to events
# sent to channel whose name begins with '/chat'
# sent to any channel whose name begins with '/chat'
class ChatListener < Alondra::EventListener

# If you want to listen to other channels than the default ones
Expand All @@ -67,9 +67,12 @@ events, such as when a client subscribe to a channel.
# This will be fire any time a client subscribe to
# any of the observed channels
on :subscribed, :to => :member do
@user = resource

# You can push notifications from listeners
# If you use Cookie Based Session Store,
# you can access the rails session from the listener
@user = User.find(session[:user_id])

# Push notifications from listener
push '/users/user', :to => channel_name
end
end
Expand Down Expand Up @@ -118,7 +121,7 @@ In the client you can listen to these events using the javacript API:

```

This technique is also especially useful if you use something like Backbone.js
This technique is especially useful if you use something like Backbone.js
to render your app frontend.


Expand Down
8 changes: 7 additions & 1 deletion lib/alondra/event_listener.rb
Expand Up @@ -64,7 +64,13 @@ def receive(event)

matching_callbacks = self.class.callbacks.find_all { |c| c.matches?(event) }
matching_callbacks.each do |callback|
self.instance_exec(event, &callback.proc)
begin
self.instance_exec(event, &callback.proc)
rescue Exception => ex
Rails.logger.error 'Error while processing event listener callback'
Rails.logger.error ex.message
Rails.logger.error ex.stacktrace if ex.respond_to? :stacktrace
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/models/event_listener_test.rb
Expand Up @@ -61,6 +61,10 @@ def self.custom_events
ChatListener.custom_events << event
end

on :boom do |event|
event.boom!
end

end


Expand Down Expand Up @@ -195,5 +199,15 @@ class OverwrittenDefaultPatternsListener < EventListener

assert_equal ChatListener.custom_events.last, event
end

test 'capture exceptions launched in event listener' do
boom = BogusEvent.new :event => :boom, :resource => Chat.new, :channel => '/chats/'
EventRouter.new.process(boom)

event = Event.new :event => :custom, :resource => Chat.new, :channel => '/chats/'
EventRouter.new.process(event)

assert_equal ChatListener.custom_events.last, event
end
end
end
5 changes: 5 additions & 0 deletions test/support/mocks/bogus_event.rb
Expand Up @@ -3,7 +3,12 @@ module Alondra
class BogusException < StandardError; end

class BogusEvent < Event

def to_json
boom!
end

def boom!
raise BogusException.new("Ha ha ha, I'm evil!")
end
end
Expand Down

0 comments on commit f7a5650

Please sign in to comment.