-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore errors in "development" and "test" environments
- Loading branch information
Showing
8 changed files
with
126 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/telebugs/rails/middleware/ignore_dev_env_middleware.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
test/telebugs/middleware/test_ignore_dev_env_middleware.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters