Skip to content
Merged
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ You can configure Solid Errors via the Rails configuration object, under the `so
* `sends_email` - Whether or not to send emails when an error occurs. See [Email notifications](#email-notifications) for more information.
* `email_from` - The email address to send a notification from. See [Email notifications](#email-notifications) for more information.
* `email_to` - The email address(es) to send a notification to. See [Email notifications](#email-notifications) for more information.
* `email_every_occurrence` - Whether to email every occurrence (the default), or only new errors and resolved errors that occur again. See [Email notifications](#email-notifications) for more information.
* `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.
Expand Down Expand Up @@ -265,6 +266,12 @@ config.solid_errors.email_subject_prefix = "[#{Rails.application.name}][#{Rails.

If you have set `send_emails` to `true` and have set an `email_to` address, Solid Errors will send an email notification whenever an error occurs. If you have not set `send_emails` to `true` or have not set an `email_to` address, Solid Errors will not send any email notifications.

To be emailed only when an error is new, or when a resolved error occurs again, rather than for every occurrence:

```ruby
config.solid_errors.email_every_occurrence = false # defaults to true
```

#### Automatically destroying old records

Setting `destroy_after` to a duration will allow Solid Errors to be self-maintaining by peridically destroying **resolved** records that are older than that value. The value provided must respond to `.ago`.
Expand Down
5 changes: 4 additions & 1 deletion app/models/solid_errors/occurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class Occurrence < Record
# Adapters that report the json column as text (MariaDB) would otherwise store Hash#inspect
attribute :context, :json

after_create_commit :send_email, if: -> { SolidErrors.send_emails? && SolidErrors.email_to.present? }
# Set by the subscriber when this occurrence creates its error or reopens a resolved one
attr_accessor :opens_error

after_create_commit :send_email, if: -> { SolidErrors.send_emails? && (SolidErrors.email_every_occurrence || opens_error) }
after_create_commit :clear_resolved_errors, if: :should_clear_resolved_errors?

# The parsed exception backtrace. Lines in this backtrace that are from installed gems
Expand Down
1 change: 1 addition & 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_from, default: "solid_errors@noreply.com"
mattr_accessor :email_to
mattr_accessor :email_subject_prefix
mattr_accessor :email_every_occurrence, default: true
mattr_accessor :destroy_after
mattr_accessor :ignored_errors, default: []

Expand Down
5 changes: 4 additions & 1 deletion lib/solid_errors/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ def report(error, handled:, severity:, context:, source: nil)
}
fingerprint = Digest::SHA256.hexdigest(error_attributes.values.join)
if (record = SolidErrors::Error.find_by(fingerprint: fingerprint))
opens_error = record.resolved?
record.update!(resolved_at: nil, updated_at: Time.now)
else
opens_error = true
record = SolidErrors::Error.create!(error_attributes.merge(fingerprint: fingerprint))
end

SolidErrors::Occurrence.create(
error_id: record.id,
backtrace: error.backtrace&.join("\n"),
context: s(context)
context: s(context),
opens_error: opens_error
)
end

Expand Down
51 changes: 51 additions & 0 deletions test/solid_errors/email_notifications_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "test_helper"

class SolidErrors::EmailNotificationsTest < ActiveSupport::TestCase
include ActionMailer::TestHelper

setup do
SolidErrors.send_emails = true
SolidErrors.email_to = "ops@example.com"
end

teardown do
SolidErrors.send_emails = false
SolidErrors.email_to = nil
SolidErrors.email_every_occurrence = true
end

test "every occurrence is emailed by default" do
report_raised_error("oof")

assert_enqueued_emails(1) { report_raised_error("oof") }
end

test "a new error is emailed when only new and reopened errors are" do
SolidErrors.email_every_occurrence = false

assert_enqueued_emails(1) { report_raised_error("oof") }
end

test "a repeat of an unresolved error is not emailed when only new and reopened errors are" do
SolidErrors.email_every_occurrence = false
report_raised_error("oof")

assert_no_enqueued_emails { report_raised_error("oof") }
end

test "a resolved error that occurs again is emailed when only new and reopened errors are" do
SolidErrors.email_every_occurrence = false
report_raised_error("oof")
SolidErrors::Error.last.update!(resolved_at: Time.current)

assert_enqueued_emails(1) { report_raised_error("oof") }
end

private

def report_raised_error(message)
raise StandardError, message
rescue => error
Rails.error.report(error)
end
end
Loading