Skip to content

Commit

Permalink
Rescue and skip error in optional_targets - #103
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Aug 13, 2019
1 parent 881e297 commit b25e230
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/activity_notification/apis/notification_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,13 @@ def publish_to_optional_targets(options = {})
notifiable.optional_targets(target.to_resources_name, key).map { |optional_target|
optional_target_name = optional_target.to_optional_target_name
if optional_target_subscribed?(optional_target_name)
optional_target.notify(self, options[optional_target_name] || {})
[optional_target_name, true]
begin
optional_target.notify(self, options[optional_target_name] || {})
[optional_target_name, true]
rescue => e
Rails.logger.error(e)
[optional_target_name, e]
end
else
[optional_target_name, false]
end
Expand Down
18 changes: 18 additions & 0 deletions spec/concerns/apis/notification_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@
end
end
end

context "when some optional targets raise error" do
before do
require 'custom_optional_targets/raise_error'
@optional_target = CustomOptionalTarget::RaiseError.new
@current_optional_target = Comment._optional_targets[:users]
Comment.acts_as_notifiable :users, optional_targets: ->{ [@optional_target] }
end

after do
Comment._optional_targets[:users] = @current_optional_target
end

it "generates notifications even if some optional targets raise error" do
notifications = described_class.notify(:users, @comment_2)
expect(notifications.size).to eq(2)
end
end
end

describe ".notify_later" do
Expand Down
14 changes: 14 additions & 0 deletions spec/rails_app/lib/custom_optional_targets/raise_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module CustomOptionalTarget
# Optional target implementation to raise error.
class RaiseError < ActivityNotification::OptionalTarget::Base
def initialize_target(options = {})
@raise_error = options[:raise_error] == false ? false : true
end

def notify(notification, options = {})
if @raise_error
raise 'Intentional RuntimeError in CustomOptionalTarget::RaiseError'
end
end
end
end

0 comments on commit b25e230

Please sign in to comment.