From 84e58cb7345beda2055ce0c3a3b111537d9f28ef Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Mon, 17 Jun 2024 16:44:56 +0300 Subject: [PATCH] Ignore errors in "development" and "test" environments --- lib/telebugs/rails.rb | 2 + .../middleware/ignore_dev_env_middleware.rb | 17 ++++++ lib/telebugs/rails/railtie.rb | 8 ++- telebugs-rails.gemspec | 2 +- .../test_ignore_dev_env_middleware.rb | 32 +++++++++++ test/telebugs/test_error_subscriber.rb | 29 +++++----- test/telebugs/test_railtie.rb | 53 +++++++++++++++++++ test/test_helper.rb | 2 +- 8 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 lib/telebugs/rails/middleware/ignore_dev_env_middleware.rb create mode 100644 test/telebugs/middleware/test_ignore_dev_env_middleware.rb create mode 100644 test/telebugs/test_railtie.rb diff --git a/lib/telebugs/rails.rb b/lib/telebugs/rails.rb index f781e42..f0f2b5d 100644 --- a/lib/telebugs/rails.rb +++ b/lib/telebugs/rails.rb @@ -6,6 +6,8 @@ require_relative "rails/version" require_relative "rails/railtie" +require_relative "rails/middleware/ignore_dev_env_middleware" + module Telebugs module Rails class Error < StandardError; end diff --git a/lib/telebugs/rails/middleware/ignore_dev_env_middleware.rb b/lib/telebugs/rails/middleware/ignore_dev_env_middleware.rb new file mode 100644 index 0000000..dee6f4a --- /dev/null +++ b/lib/telebugs/rails/middleware/ignore_dev_env_middleware.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Telebugs::Rails::Middleware + class IgnoreDevEnvMiddleware < Telebugs::Middleware + def initialize(rails_env) + @rails_env = rails_env.to_s + end + + def call(report) + report.ignored = (@rails_env == "development" || @rails_env == "test") + end + + def weight + -1000 + end + end +end diff --git a/lib/telebugs/rails/railtie.rb b/lib/telebugs/rails/railtie.rb index 8e29b0d..39fc265 100644 --- a/lib/telebugs/rails/railtie.rb +++ b/lib/telebugs/rails/railtie.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module Telebugs +module Telebugs::Rails class Railtie < ::Rails::Railtie # https://guides.rubyonrails.org/error_reporting.html initializer "telebugs.error_subscribe" do @@ -10,5 +10,11 @@ class Railtie < ::Rails::Railtie require "telebugs/rails/error_subscriber" ::Rails.error.subscribe(Telebugs::Rails::ErrorSubscriber.new) end + + initializer "telebugs.configure" do + Telebugs.configure do |c| + c.middleware.use Middleware::IgnoreDevEnvMiddleware.new(Rails.env) + end + end end end diff --git a/telebugs-rails.gemspec b/telebugs-rails.gemspec index 035e2b0..06b079a 100644 --- a/telebugs-rails.gemspec +++ b/telebugs-rails.gemspec @@ -32,7 +32,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_dependency "telebugs", "~> 0.3" + spec.add_dependency "telebugs", "~> 0.4" spec.add_dependency "railties", ">= 6.1" # For more information and examples about making a new gem, check out our diff --git a/test/telebugs/middleware/test_ignore_dev_env_middleware.rb b/test/telebugs/middleware/test_ignore_dev_env_middleware.rb new file mode 100644 index 0000000..6e378da --- /dev/null +++ b/test/telebugs/middleware/test_ignore_dev_env_middleware.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "test_helper" + +class Telebugs::Rails::Middleware::TestIgnoreDevEnvMiddleware < Minitest::Test + def test_ignore_dev_env_middleware_ignores_development_errors + middleware = Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware.new("development") + + report = Telebugs::Report.new(StandardError.new("test error")) + middleware.call(report) + + assert report.ignored + end + + def test_ignore_dev_env_middleware_ignores_test_errors + middleware = Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware.new("test") + + report = Telebugs::Report.new(StandardError.new("test error")) + middleware.call(report) + + assert report.ignored + end + + def test_ignore_dev_env_middleware_does_not_ignore_production_errors + middleware = Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware.new("production") + + report = Telebugs::Report.new(StandardError.new("test error")) + middleware.call(report) + + refute report.ignored + end +end diff --git a/test/telebugs/test_error_subscriber.rb b/test/telebugs/test_error_subscriber.rb index 3787167..ffdb7f0 100644 --- a/test/telebugs/test_error_subscriber.rb +++ b/test/telebugs/test_error_subscriber.rb @@ -2,7 +2,7 @@ require "test_helper" -class Telebugs::TestErrorSubscriber < Minitest::Test +class Telebugs::Rails::TestErrorSubscriber < Minitest::Test def setup @stub = stub_request(:post, Telebugs.config.api_url) .to_return(status: 201, body: "{}") @@ -13,20 +13,17 @@ def teardown end def test_error_subscriber_subscribes_to_rails_error - skip("Rails 7.0 and later only") unless Rails.version.to_f >= 7.0 - - stub = stub_request(:post, Telebugs.config.api_url) - .to_return(status: 201, body: "{}") - - if Rails.version.to_f == 7.0 # rubocop:disable Lint/FloatComparison - Rails.error.report(RuntimeError.new("test railstie"), handled: true) - else - Rails.error.report(RuntimeError.new("test railstie")) - end - - # Wait for the subscriber to process the error since it's async. - sleep 0.01 - - assert_requested stub + skip "Rails 7.0 and later only" unless Rails.version.to_f >= 7.0 + + error_subscriber = Telebugs::Rails::ErrorSubscriber.new + p = error_subscriber.report( + RuntimeError.new("test error"), + handled: true, + severity: "error", + context: {foo: "bar"} + ) + p.wait + + assert_requested @stub end end diff --git a/test/telebugs/test_railtie.rb b/test/telebugs/test_railtie.rb new file mode 100644 index 0000000..4c795d7 --- /dev/null +++ b/test/telebugs/test_railtie.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "test_helper" + +class Telebugs::Rails::TestRailtie < Minitest::Test + def setup + @stub = stub_request(:post, Telebugs.config.api_url) + .to_return(status: 201, body: "{}") + @env = Rails.env + end + + def teardown + Rails.env = @env + Telebugs.config.middleware.delete Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware + Telebugs.config.middleware.use Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware.new(Rails.env) + + WebMock.reset! + end + + def test_error_subscriber_subscribes_to_rails_error + skip("Rails 7.0 and later only") unless Rails.version.to_f >= 7.0 + + if Rails.version.to_f == 7.0 # rubocop:disable Lint/FloatComparison + Rails.error.report(RuntimeError.new("test railstie"), handled: true) + else + Rails.error.report(RuntimeError.new("test railstie")) + end + + # Wait for the subscriber to process the error since it's async. + sleep 0.01 + + assert_requested @stub + end + + def test_error_subscriber_respects_the_ignore_dev_middleware + skip("Rails 7.0 and later only") unless Rails.version.to_f >= 7.0 + + Rails.env = "development" + Telebugs.config.middleware.delete Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware + Telebugs.config.middleware.use Telebugs::Rails::Middleware::IgnoreDevEnvMiddleware.new(Rails.env) + + if Rails.version.to_f == 7.0 # rubocop:disable Lint/FloatComparison + Rails.error.report(RuntimeError.new("test ignore env"), handled: true) + else + Rails.error.report(RuntimeError.new("test ignore env")) + end + + # Wait for the subscriber to process the error since it's async. + sleep 0.01 + + refute_requested @stub + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 35c165f..b89f669 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,7 +8,7 @@ puts "Rails/#{Rails.version}" -ENV["RAILS_ENV"] = "test" +ENV["RAILS_ENV"] = "production" class TestApp < Rails::Application config.eager_load = false