Skip to content

Commit

Permalink
Merge pull request #6 from telebugs/ignore-env-middleware
Browse files Browse the repository at this point in the history
Ignore errors in "development" and "test" environments
  • Loading branch information
kyrylo authored Jun 17, 2024
2 parents 2992fcc + 84e58cb commit 375966e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lib/telebugs/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/telebugs/rails/middleware/ignore_dev_env_middleware.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion lib/telebugs/rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion telebugs-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/telebugs/middleware/test_ignore_dev_env_middleware.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 13 additions & 16 deletions test/telebugs/test_error_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "{}")
Expand All @@ -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
53 changes: 53 additions & 0 deletions test/telebugs/test_railtie.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

puts "Rails/#{Rails.version}"

ENV["RAILS_ENV"] = "test"
ENV["RAILS_ENV"] = "production"

class TestApp < Rails::Application
config.eager_load = false
Expand Down

0 comments on commit 375966e

Please sign in to comment.