From 6403a334c6dfb49328a0615125864a8aecb44cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20H=C3=A4usele?= Date: Thu, 11 Jan 2024 19:36:25 +0100 Subject: [PATCH 1/6] Add CI for 7.0 and 7.1 (#7) * Add CI for 7.0 and 7.1 --- .github/workflows/ruby.yml | 13 +++----- Appraisals | 7 +++++ Gemfile | 2 ++ email_error_reporter.gemspec | 3 +- gemfiles/rails_7.0.gemfile | 12 +++++++ gemfiles/rails_7.1.gemfile | 12 +++++++ test/dummy/config/application.rb | 3 +- test/dummy/config/environments/development.rb | 2 +- test/dummy/config/environments/test.rb | 2 +- test/subscriber_test.rb | 31 ++++++++++++++----- 10 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 Appraisals create mode 100644 gemfiles/rails_7.0.gemfile create mode 100644 gemfiles/rails_7.1.gemfile diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 1a40cb0..f9ff3bf 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -7,11 +7,7 @@ name: Tests -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] +on: [push, pull_request] permissions: contents: read @@ -23,13 +19,14 @@ jobs: strategy: matrix: ruby-version: ['3.1', '3.2', '3.3'] + rails-version: ['gemfiles/rails_7.0.gemfile', 'gemfiles/rails_7.1.gemfile'] + + env: + BUNDLE_GEMFILE: ${{ matrix.rails-version }} steps: - uses: actions/checkout@v3 - name: Set up Ruby - # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby, - # change this to (see https://github.com/ruby/setup-ruby#versioning): - # uses: ruby/setup-ruby@v1 uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby-version }} diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..6548bad --- /dev/null +++ b/Appraisals @@ -0,0 +1,7 @@ +appraise "rails-7.0" do + gem "rails", "~> 7.0.0" +end + +appraise "rails-7.1" do + gem "rails", "~> 7.1.0" +end diff --git a/Gemfile b/Gemfile index f1fe10a..e231bf5 100644 --- a/Gemfile +++ b/Gemfile @@ -12,3 +12,5 @@ gem "sqlite3" gem "debug", ">= 1.0.0" gem "rails-dom-testing" +gem "appraisal" + diff --git a/email_error_reporter.gemspec b/email_error_reporter.gemspec index 72c9a47..a10548b 100644 --- a/email_error_reporter.gemspec +++ b/email_error_reporter.gemspec @@ -22,5 +22,6 @@ Gem::Specification.new do |spec| Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] end - spec.add_dependency "rails", ">= 7.1.2" + spec.required_ruby_version = '>= 3.1.0' + spec.add_dependency "rails", ">= 7.0.0" end diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile new file mode 100644 index 0000000..ec6188e --- /dev/null +++ b/gemfiles/rails_7.0.gemfile @@ -0,0 +1,12 @@ +# This file was generated by Appraisal + +source "https://rubygems.org/" + +gem "puma" +gem "sqlite3" +gem "debug", ">= 1.0.0" +gem "rails-dom-testing" +gem "appraisal", group: :development +gem "rails", "~> 7.0.0" + +gemspec path: "../" diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile new file mode 100644 index 0000000..1cce9dc --- /dev/null +++ b/gemfiles/rails_7.1.gemfile @@ -0,0 +1,12 @@ +# This file was generated by Appraisal + +source "https://rubygems.org/" + +gem "puma" +gem "sqlite3" +gem "debug", ">= 1.0.0" +gem "rails-dom-testing" +gem "appraisal", group: :development +gem "rails", "~> 7.1.0" + +gemspec path: "../" diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index 5295e62..8dfe5e0 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -13,7 +13,8 @@ class Application < Rails::Application # Please, add to the `ignore` list any other `lib` subdirectories that do # not contain `.rb` files, or that should not be reloaded or eager loaded. # Common ones are `templates`, `generators`, or `middleware`, for example. - config.autoload_lib(ignore: %w(assets tasks)) + + # config.autoload_lib(ignore: %w(assets tasks)) # Configuration for the application, engines, and railties goes here. # diff --git a/test/dummy/config/environments/development.rb b/test/dummy/config/environments/development.rb index 84e2859..21a448d 100644 --- a/test/dummy/config/environments/development.rb +++ b/test/dummy/config/environments/development.rb @@ -72,7 +72,7 @@ # config.action_cable.disable_request_forgery_protection = true # Raise error when a before_action's only/except options reference missing actions - config.action_controller.raise_on_missing_callback_actions = true + # config.action_controller.raise_on_missing_callback_actions = true config.email_error_reporter.to = ["test@example.com"] end diff --git a/test/dummy/config/environments/test.rb b/test/dummy/config/environments/test.rb index ee40759..afe54f6 100644 --- a/test/dummy/config/environments/test.rb +++ b/test/dummy/config/environments/test.rb @@ -60,7 +60,7 @@ # config.action_view.annotate_rendered_view_with_filenames = true # Raise error when a before_action's only/except options reference missing actions - config.action_controller.raise_on_missing_callback_actions = true + # config.action_controller.raise_on_missing_callback_actions = true config.email_error_reporter.to = ["test@example.com"] end diff --git a/test/subscriber_test.rb b/test/subscriber_test.rb index c9c4fbd..27b4016 100644 --- a/test/subscriber_test.rb +++ b/test/subscriber_test.rb @@ -11,13 +11,28 @@ class SubscriberTest < ActiveSupport::TestCase end rescue TestError => e - assert_enqueued_email_with EmailErrorReporter::ErrorMailer, :error, args: ->(args) { - # TODO: find out, why args.first == e is false - [ - args.first.message == e.message, - args.first.backtrace == e.backtrace, - args.first.class == e.class - ].all? - } + # matcher + assert_enqueued_with( + job: ActionMailer::MailDeliveryJob, + args: ->(j) { + [ + "EmailErrorReporter::ErrorMailer" == j[0], + "error" == j[1], + "deliver_now" == j[2], + e.class == j[3][:args][0].class, + { handled: false, context: {}, severity: :error, source: rails_default_source } == j[3][:args][1], + ].all? + } + ) + end + + private + + def rails_default_source + if Gem::Version.new(Rails.version) >= Gem::Version.new("7.1.0") + "application" + else + nil + end end end From 0e26f5a974890689d6cc25d288529b77352d8e13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20H=C3=A4usele?= Date: Thu, 11 Jan 2024 23:21:45 +0100 Subject: [PATCH 2/6] Set a default from address to avoid errors (#9) --- README.md | 9 +++++++++ lib/email_error_reporter/engine.rb | 1 + lib/email_error_reporter/error_mailer.rb | 1 + 3 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 36b5bf1..8fed9e0 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,15 @@ All exceptions are reported automatically. No additional code required. Please consult the [official guides](https://guides.rubyonrails.org/error_reporting.html) for an introduction to the error reporting API. +## Optional configuration + +Set a custom from address. + +```ruby +# default: "no-reply@example.com" +config.email_error_reporter.from = "your-custom-email@example.com" +``` + ## Test your setup You can use the built-in rake task `rake email_error_reporter:check` to check if everything works correctly in your setup. diff --git a/lib/email_error_reporter/engine.rb b/lib/email_error_reporter/engine.rb index b914d07..b0d84c2 100644 --- a/lib/email_error_reporter/engine.rb +++ b/lib/email_error_reporter/engine.rb @@ -4,6 +4,7 @@ class Engine < ::Rails::Engine config.email_error_reporter = ActiveSupport::OrderedOptions.new config.email_error_reporter.to = [] + config.email_error_reporter.from = "no-reply@example.com" initializer "email_error_reporter.error_subscribe" do Rails.error.subscribe(Subscriber.new) diff --git a/lib/email_error_reporter/error_mailer.rb b/lib/email_error_reporter/error_mailer.rb index c560523..0df2c2a 100644 --- a/lib/email_error_reporter/error_mailer.rb +++ b/lib/email_error_reporter/error_mailer.rb @@ -17,6 +17,7 @@ def error(error, handled:, severity:, context:, source: nil) mail( subject: "#{severity_to_emoji.fetch(@severity)} #{error.class}", + from: Rails.application.config.email_error_reporter.from, to: Rails.application.config.email_error_reporter.to ) end From 46d7b7e9f620e7fbc311a95240109c2c952aacd0 Mon Sep 17 00:00:00 2001 From: Niklas Haeusele Date: Thu, 11 Jan 2024 23:23:18 +0100 Subject: [PATCH 3/6] bump 0.2.0 --- lib/email_error_reporter/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/email_error_reporter/version.rb b/lib/email_error_reporter/version.rb index b0651e7..e8e661b 100644 --- a/lib/email_error_reporter/version.rb +++ b/lib/email_error_reporter/version.rb @@ -1,3 +1,3 @@ module EmailErrorReporter - VERSION = "0.1.0.pre" + VERSION = "0.2.0" end From 4b99a976db1552a111a9aafc335e0a8cfc0cd8e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20H=C3=A4usele?= Date: Mon, 15 Jan 2024 22:45:23 +0100 Subject: [PATCH 4/6] Add .enabled configuration (#10) --- README.md | 10 +++++++++- lib/email_error_reporter/engine.rb | 7 +++++-- test/subscriber_test.rb | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8fed9e0..f43e6bc 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Add the gem: ```bash -bundle add email_error_reporter && bundle install +bundle add email_error_reporter ``` and configure the email addresses that should receive an email in the case of an exception: @@ -37,6 +37,14 @@ Set a custom from address. config.email_error_reporter.from = "your-custom-email@example.com" ``` +Disables the email reports for specific environments. Mails are enabled by default on all envs. + +```ruby +# e.g. in development.rb +# default: true +config.email_error_reporter.enabled = false +``` + ## Test your setup You can use the built-in rake task `rake email_error_reporter:check` to check if everything works correctly in your setup. diff --git a/lib/email_error_reporter/engine.rb b/lib/email_error_reporter/engine.rb index b0d84c2..d6a2ea4 100644 --- a/lib/email_error_reporter/engine.rb +++ b/lib/email_error_reporter/engine.rb @@ -5,9 +5,12 @@ class Engine < ::Rails::Engine config.email_error_reporter = ActiveSupport::OrderedOptions.new config.email_error_reporter.to = [] config.email_error_reporter.from = "no-reply@example.com" + config.email_error_reporter.enabled = true - initializer "email_error_reporter.error_subscribe" do - Rails.error.subscribe(Subscriber.new) + initializer "email_error_reporter.error_subscribe" do |app| + if app.config.email_error_reporter.enabled + Rails.error.subscribe(Subscriber.new) + end end # ActiveJob cannot (de)-serialize exceptions by default diff --git a/test/subscriber_test.rb b/test/subscriber_test.rb index 27b4016..ce3df2c 100644 --- a/test/subscriber_test.rb +++ b/test/subscriber_test.rb @@ -26,6 +26,31 @@ class SubscriberTest < ActiveSupport::TestCase ) end + test "enqueues no mail if disabled" do + old_config = Rails.application.config.email_error_reporter.enabled + Rails.application.config.email_error_reporter.enabled = false + Rails.error.record(TestError) do + raise TestError + end + + rescue TestError => e + # matcher + assert_enqueued_with( + job: ActionMailer::MailDeliveryJob, + args: ->(j) { + [ + "EmailErrorReporter::ErrorMailer" == j[0], + "error" == j[1], + "deliver_now" == j[2], + e.class == j[3][:args][0].class, + { handled: false, context: {}, severity: :error, source: rails_default_source } == j[3][:args][1], + ].all? + } + ) + ensure + Rails.application.config.email_error_reporter.enabled = old_config + end + private def rails_default_source From a7674ffa5a45e0bef20d940ad1fe6795563d2af8 Mon Sep 17 00:00:00 2001 From: Niklas Haeusele Date: Mon, 15 Jan 2024 22:50:11 +0100 Subject: [PATCH 5/6] bump 0.3.0 --- lib/email_error_reporter/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/email_error_reporter/version.rb b/lib/email_error_reporter/version.rb index e8e661b..917bf46 100644 --- a/lib/email_error_reporter/version.rb +++ b/lib/email_error_reporter/version.rb @@ -1,3 +1,3 @@ module EmailErrorReporter - VERSION = "0.2.0" + VERSION = "0.3.0" end From c4f983e303531d9f3bbcd8b0bad5fba21766de7e Mon Sep 17 00:00:00 2001 From: Niklas Haeusele Date: Mon, 15 Jan 2024 22:59:02 +0100 Subject: [PATCH 6/6] README fixes --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f43e6bc..ea12783 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,9 @@ Set a custom from address. config.email_error_reporter.from = "your-custom-email@example.com" ``` -Disables the email reports for specific environments. Mails are enabled by default on all envs. +--- + +Disables the email reports for specific environments. ```ruby # e.g. in development.rb @@ -51,10 +53,12 @@ You can use the built-in rake task `rake email_error_reporter:check` to check if If you're using Kamal, you can run the following command to test the setup in production: -``` +```sh kamal app exec -p 'bundle exec rake email_error_reporter:test_email' ``` +If everything is setup correctly, this rake task will send a demo email to your configured `to` address with a sample exception. + ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).