Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,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` - An array of error class names (strings) that will not be saved in addition to the default list hardcoded in `lib/solid_errors/subscriber.rb`

### Database Configuration

Expand Down Expand Up @@ -376,4 +377,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).

4 changes: 4 additions & 0 deletions app/models/solid_errors/occurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Occurrence < Record
after_create_commit :send_email, if: -> { SolidErrors.send_emails? && SolidErrors.email_to.present? }
after_create_commit :clear_resolved_errors, if: :should_clear_resolved_errors?

unless type_for_attribute("context").is_a?(ActiveRecord::Type::Json)
serialize :context, coder: JSON
end

# The parsed exception backtrace. Lines in this backtrace that are from installed gems
# have the base path for gem installs replaced by "[GEM_ROOT]", while those in the project
# have "[PROJECT_ROOT]".
Expand Down
5 changes: 5 additions & 0 deletions lib/solid_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module SolidErrors
mattr_accessor :email_to
mattr_accessor :email_subject_prefix
mattr_accessor :destroy_after
mattr_writer :ignored_errors

class << self
# use method instead of attr_accessor to ensure
Expand All @@ -29,6 +30,10 @@ def password
@password ||= ENV["SOLIDERRORS_PASSWORD"] || @@password
end

def ignored_errors
@ignored_errors ||= (@@ignored_errors || []).to_h{|k| [k,true]}
end

def send_emails?
send_emails && email_to.present?
end
Expand Down
9 changes: 2 additions & 7 deletions lib/solid_errors/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class Subscriber
"CGI::Session::CookieStore::TamperedWithCookie",
"Mongoid::Errors::DocumentNotFound",
"Sinatra::NotFound",
"Sidekiq::JobRetry::Skip"].map(&:freeze).freeze
"Sidekiq::JobRetry::Skip"].to_h{|v| [v.freeze,true]}.freeze

def report(error, handled:, severity:, context:, source: nil)
return if ignore_by_class?(error.class.name)

error_attributes = {
exception_class: error.class.name,
message: s(error.message),
Expand All @@ -49,11 +48,7 @@ def s(data)
end

def ignore_by_class?(error_class_name)
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
end
SolidErrors.ignored_errors.key?(error_class_name) || IGNORED_ERRORS.key?(error_class_name)
end
end
end
23 changes: 23 additions & 0 deletions test/models/solid_errors/occurrence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,34 @@ def teardown
end
end

test "an ignored error should not be saved while an acceptable one is saved" do
SolidErrors.ignored_errors = ["RuntimeError"]
# An hardcoded ignored error should be discarded
assert_difference -> { SolidErrors::Error.count }, 0 do
assert_difference -> { SolidErrors::Occurrence.count }, 0 do
Rails.error.report( ActionController::RoutingError.new('argh'))
end
end
# A user ignored error should be discarded
assert_difference -> { SolidErrors::Error.count }, 0 do
assert_difference -> { SolidErrors::Occurrence.count }, 0 do
Rails.error.report( RuntimeError.new("argh") )
end
end
# A valid error should be recorded
assert_difference -> { SolidErrors::Error.count }, +1 do
assert_difference -> { SolidErrors::Occurrence.count }, +1 do
Rails.error.report(StandardError.new("argh"))
end
end
end

private

def simulate_99_old_exceptions(status)
Rails.error.report(StandardError.new("argh"))
SolidErrors::Error.update_all(resolved_at: Time.current) if status == :resolved
SolidErrors::Occurrence.last.update!(id: 99, created_at: 1.day.ago)
end

end