Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email notifications #3

Merged
merged 13 commits into from
Feb 9, 2024
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/spec/reports/
/tmp/
.DS_Store
test/dummy/log/*.log
2 changes: 1 addition & 1 deletion .standard.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# For available configuration options, see:
# https://github.com/testdouble/standard
ruby_version: 2.6
ruby_version: 3.0
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ There are intentionally few features; you can view and resolve errors. That’s

### Configuration

You can configure Solid Errors via the Rails configuration object, under the `solid_errors` key. Currently, only 3 configuration options are available:
You can configure Solid Errors via the Rails configuration object, under the `solid_errors` key. Currently, 6 configuration options are available:

* `connects_to` - The database configuration to use for the Solid Errors database. See [Database Configuration](#database-configuration) for more information.
* `username` - The username to use for HTTP authentication. See [Authentication](#authentication) for more information.
* `password` - The password to use for HTTP authentication. See [Authentication](#authentication) for more information.
* `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.

#### Database Configuration

Expand Down Expand Up @@ -117,6 +120,29 @@ authenticate :user, -> (user) { user.admin? } do
end
```

#### Email notifications

Solid Errors _can_ send email notifications whenever an error occurs, if your application has ActionMailer already properly setup to send emails. However, in order to activate this feature you must define the email address(es) to send the notifications to. Optionally, you can also define the email address to send the notifications from (useful if your email provider only allows emails to be sent from a predefined list of addresses) or simply turn off this feature altogether.

There are two ways to configure email notifications. First, you can use environment variables:

```ruby
ENV["SOLIDERRORS_SEND_EMAILS"] = true # defaults to true
ENV["SOLIDERRORS_EMAIL_FROM"] = "errors@myapp.com" # defaults to "solid_errors@noreply.com"
ENV["SOLIDERRORS_EMAIL_TO"] = "devs@myapp.com" # no default, must be set
```

Second, you can set the values via the configuration object:

```ruby
# Set authentication credentials for Solid Errors
config.solid_errors.send_emails = true
config.solid_errors.email_from = "errors@myapp.com"
config.solid_errors.email_to = "devs@myapp.com"
```

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.

### Examples

There are only two screens in the dashboard.
Expand Down
15 changes: 15 additions & 0 deletions app/mailers/solid_errors/error_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module SolidErrors
# adapted from: https://github.com/codergeek121/email_error_reporter/blob/main/lib/email_error_reporter/error_mailer.rb
class ErrorMailer < ActionMailer::Base
def error_occurred(occurrence)
@occurrence = occurrence
@error = occurrence.error

mail(
subject: "#{@error.emoji} #{@error.exception_class}",
from: SolidErrors.email_from,
to: SolidErrors.email_to
)
end
end
end
2 changes: 1 addition & 1 deletion app/models/solid_errors/backtrace_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SolidErrors
# adapted from: https://github.com/honeybadger-io/honeybadger-ruby/blob/master/lib/honeybadger/backtrace.rb
class BacktraceLine
# Backtrace line regexp (optionally allowing leading X: for windows support).
INPUT_FORMAT = %r{^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$}.freeze
INPUT_FORMAT = %r{^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$}
STRING_EMPTY = "".freeze
GEM_ROOT = "[GEM_ROOT]".freeze
PROJECT_ROOT = "[PROJECT_ROOT]".freeze
Expand Down
6 changes: 6 additions & 0 deletions app/models/solid_errors/occurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module SolidErrors
class Occurrence < Record
belongs_to :error, class_name: "SolidErrors::Error"

after_create_commit :send_email, if: -> { SolidErrors.send_emails? && SolidErrors.email_to.present? }

# 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 All @@ -17,5 +19,9 @@ def parsed_backtrace
def parse_backtrace(backtrace)
Backtrace.parse(backtrace)
end

def send_email
ErrorMailer.error_occurred(self).deliver_later
end
end
end
Loading
Loading