diff --git a/README.md b/README.md index 1cedce4..ef1635c 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ You can configure Solid Errors via the Rails configuration object, under the `so * `email_subject_prefix` - Prefix added to the subject line for email notifications. See [Email notifications](#email-notifications) for more information. * `base_controller_class` - Specify a different controller as the base class for the Solid Errors controller. See [Authentication](#authentication) for more information. * `destroy_after` - If set, Solid Errors will periodically destroy resolved records that are older than the value specified. See [Automatically destroying old records](#automatically-destroying-old-records) for more information. +* `ignored_errors` - Error classes, or their names, to never record, in addition to the built-in list in `SolidErrors::Subscriber::IGNORED_ERRORS`. For example, `config.solid_errors.ignored_errors = ["ActiveStorage::FileNotFoundError"]`. ### Database Configuration @@ -388,4 +389,3 @@ The gem is available as open source under the terms of the [MIT License](https:/ ## Code of Conduct Everyone interacting in the SolidErrors project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fractaledmind/solid_errors/blob/main/CODE_OF_CONDUCT.md). - diff --git a/lib/solid_errors.rb b/lib/solid_errors.rb index f89f07b..6d70119 100644 --- a/lib/solid_errors.rb +++ b/lib/solid_errors.rb @@ -16,6 +16,7 @@ module SolidErrors mattr_accessor :email_to mattr_accessor :email_subject_prefix mattr_accessor :destroy_after + mattr_accessor :ignored_errors, default: [] class << self # use method instead of attr_accessor to ensure diff --git a/lib/solid_errors/subscriber.rb b/lib/solid_errors/subscriber.rb index 2063250..21ac5a3 100644 --- a/lib/solid_errors/subscriber.rb +++ b/lib/solid_errors/subscriber.rb @@ -49,7 +49,7 @@ def s(data) end def ignore_by_class?(error_class_name) - IGNORED_ERRORS.any? do |ignored_class| + [*IGNORED_ERRORS, *SolidErrors.ignored_errors].any? do |ignored_class| ignored_class_name = ignored_class.respond_to?(:name) ? ignored_class.name : ignored_class ignored_class_name == error_class_name diff --git a/test/solid_errors/subscriber_test.rb b/test/solid_errors/subscriber_test.rb new file mode 100644 index 0000000..68e73e0 --- /dev/null +++ b/test/solid_errors/subscriber_test.rb @@ -0,0 +1,34 @@ +require "test_helper" + +class SolidErrors::SubscriberTest < ActiveSupport::TestCase + CustomError = Class.new(StandardError) + + teardown do + SolidErrors.ignored_errors = [] + end + + test "errors named in ignored_errors are not recorded while others are" do + SolidErrors.ignored_errors = [CustomError.name] + + Rails.error.report(CustomError.new("ignored")) + Rails.error.report(StandardError.new("recorded")) + + assert_equal ["StandardError"], SolidErrors::Error.pluck(:exception_class) + end + + test "ignored_errors accepts classes as well as names" do + SolidErrors.ignored_errors = [CustomError] + + Rails.error.report(CustomError.new("ignored")) + + assert_empty SolidErrors::Error.all + end + + test "the built-in ignored errors still apply alongside ignored_errors" do + SolidErrors.ignored_errors = [CustomError.name] + + Rails.error.report(ActiveRecord::RecordNotFound.new("ignored")) + + assert_empty SolidErrors::Error.all + end +end