Skip to content

Commit

Permalink
Add rescue_optional_target_errors option - #155 #156
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Feb 12, 2022
1 parent 233b013 commit bc9da22
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/activity_notification/apis/notification_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ def publish_to_optional_targets(options = {})
[optional_target_name, true]
rescue => e
Rails.logger.error(e)
raise e
if ActivityNotification.config.rescue_optional_target_errors
[optional_target_name, e]
else
raise e
end
end
else
[optional_target_name, false]
Expand Down
10 changes: 10 additions & 0 deletions lib/activity_notification/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ class Config
# @return [String] Notification API channel prefix for ActionCable.
attr_accessor :notification_api_channel_prefix

# @overload rescue_optional_target_errors
# Returns whether activity_notification internally rescues optional target errors
# @return [Boolean] Whether activity_notification internally rescues optional target errors.
# @overload rescue_optional_target_errors=(value)
# Sets whether activity_notification internally rescues optional target errors
# @param [Boolean] rescue_optional_target_errors The new rescue_optional_target_errors
# @return [Boolean] Whether activity_notification internally rescues optional target errors.
attr_accessor :rescue_optional_target_errors

# Initialize configuration for ActivityNotification.
# These configuration can be overridden in initializer.
# @return [Config] A new instance of Config
Expand Down Expand Up @@ -242,6 +251,7 @@ def initialize
@action_cable_with_devise = false
@notification_channel_prefix = 'activity_notification_channel'
@notification_api_channel_prefix = 'activity_notification_api_channel'
@rescue_optional_target_errors = true
end

# Sets ORM name for ActivityNotification (:active_record, :mongoid or :dynamodb)
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/templates/activity_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@
# Configure notification API channel prefix for ActionCable.
config.notification_api_channel_prefix = 'activity_notification_api_channel'

# Configure if activity_notification internally rescues optional target errors. Default value is true.
# See https://github.com/simukappu/activity_notification/issues/155 for more details.
config.rescue_optional_target_errors = true

end
19 changes: 17 additions & 2 deletions spec/concerns/apis/notification_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,23 @@
Comment._optional_targets[:users] = @current_optional_target
end

it "raises an capturable exception" do
expect { described_class.notify(:users, @comment_2) }.to raise_error(RuntimeError)
context "with true as ActivityNotification.config.rescue_optional_target_errors" do
it "generates notifications even if some optional targets raise error" do
rescue_optional_target_errors = ActivityNotification.config.rescue_optional_target_errors
ActivityNotification.config.rescue_optional_target_errors = true
notifications = described_class.notify(:users, @comment_2)
expect(notifications.size).to eq(2)
ActivityNotification.config.rescue_optional_target_errors = rescue_optional_target_errors
end
end

context "with false as ActivityNotification.config.rescue_optional_target_errors" do
it "raises an capturable exception" do
rescue_optional_target_errors = ActivityNotification.config.rescue_optional_target_errors
ActivityNotification.config.rescue_optional_target_errors = false
expect { described_class.notify(:users, @comment_2) }.to raise_error(RuntimeError)
ActivityNotification.config.rescue_optional_target_errors = rescue_optional_target_errors
end
end

it "allows an exception to be captured to continue" do
Expand Down
4 changes: 4 additions & 0 deletions spec/rails_app/config/initializers/activity_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@
# Configure notification API channel prefix for ActionCable.
config.notification_api_channel_prefix = 'activity_notification_api_channel'

# Configure if activity_notification internally rescues optional target errors. Default value is true.
# See https://github.com/simukappu/activity_notification/issues/155 for more details.
config.rescue_optional_target_errors = true

end

0 comments on commit bc9da22

Please sign in to comment.