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

Add IgnoreEnvironments middleware #31

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/telebugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

require_relative "telebugs/middleware/gem_root_filter"
require_relative "telebugs/middleware/root_directory_filter"
require_relative "telebugs/middleware/ignore_environments"

module Telebugs
# The general error that this library uses when it wants to raise.
Expand Down
43 changes: 28 additions & 15 deletions lib/telebugs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Config
:middleware

attr_reader :api_url,
:root_directory
:root_directory,
:ignore_environments,
:environment

class << self
attr_writer :instance
Expand All @@ -22,30 +24,41 @@ def initialize
reset
end

def reset
self.api_key = nil
self.api_url = ERROR_API_URL

@middleware = MiddlewareStack.new
@middleware.use Middleware::GemRootFilter.new

self.root_directory = (defined?(Bundler) && Bundler.root) || Dir.pwd
self.environment = ""
self.ignore_environments = []
end

def api_url=(url)
@api_url = URI(url)
end

def root_directory=(directory)
@root_directory = File.realpath(directory)

if @middleware
@middleware.delete(Middleware::RootDirectoryFilter)
@middleware.use Middleware::RootDirectoryFilter.new(@root_directory)
end
@middleware.delete(Middleware::RootDirectoryFilter)
@middleware.use Middleware::RootDirectoryFilter.new(@root_directory)
end

def reset
self.api_key = nil
self.api_url = ERROR_API_URL
self.root_directory = File.realpath(
(defined?(Bundler) && Bundler.root) ||
Dir.pwd
)
def environment=(environment)
@environment = environment

@middleware = MiddlewareStack.new
@middleware.use Middleware::GemRootFilter.new
@middleware.use Middleware::RootDirectoryFilter.new(root_directory)
@middleware.delete(Middleware::IgnoreEnvironments)
@middleware.use Middleware::IgnoreEnvironments.new(@environment, @ignore_environments)
end

def ignore_environments=(ignore_environments)
@ignore_environments = ignore_environments

@middleware.delete(Middleware::IgnoreEnvironments)
@middleware.use Middleware::IgnoreEnvironments.new(@environment, @ignore_environments)
end
end
end
20 changes: 20 additions & 0 deletions lib/telebugs/middleware/ignore_environments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Telebugs
class Middleware
class IgnoreEnvironments < Telebugs::Middleware
def initialize(current_env, ignore_envs)
@current_env = current_env
@ignore_envs = ignore_envs
end

def call(report)
report.ignored = @ignore_envs.include?(@current_env)
end

def weight
-1000
end
end
end
end
2 changes: 1 addition & 1 deletion lib/telebugs/middleware_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize
end

def use(new_middleware)
@middlewares = (@middlewares << new_middleware).sort_by(&:weight).reverse
@middlewares = (@middlewares << new_middleware).sort_by(&:weight)
end

def delete(middleware_class)
Expand Down
4 changes: 2 additions & 2 deletions lib/telebugs/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def instance

def initialize
@sender = Sender.new
@middleware = Config.instance.middleware
@config = Config.instance
end

def report(error)
Telebugs::Promise.new(error) do
report = Report.new(error)

@middleware.call(report)
@config.middleware.call(report)
next if report.ignored

@sender.send(report)
Expand Down
23 changes: 23 additions & 0 deletions test/middleware/test_ignore_environments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "test_helper"

class TestIgnoreEnvironments < Minitest::Test
def test_ignore_environments_when_env_matches
report = Telebugs::Report.new(StandardError.new("test error"))
Telebugs::Middleware::IgnoreEnvironments.new("production", ["production"]).call(report)

assert report.ignored
end

def test_ignore_environments_when_env_does_not_match
report = Telebugs::Report.new(StandardError.new("test error"))
Telebugs::Middleware::IgnoreEnvironments.new("development", ["production"]).call(report)

refute report.ignored
end

def test_weight
assert_equal(-1000, Telebugs::Middleware::IgnoreEnvironments.new("production", []).weight)
end
end
20 changes: 16 additions & 4 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def test_error_api_url
end

def test_root_directory
Telebugs.configure { |c| c.root_directory = "/etc" }
Telebugs.configure { |c| c.root_directory = "/usr" }

assert_equal "/etc", Telebugs.config.root_directory
assert_equal "/usr", Telebugs.config.root_directory
end

def test_root_directory_overwrite_root_directory_filter_middleware
Expand All @@ -33,7 +33,7 @@ def test_root_directory_overwrite_root_directory_filter_middleware
c.root_directory = "/etc"
end

assert_equal 2, Telebugs.config.middleware.middlewares.size
assert_equal 3, Telebugs.config.middleware.middlewares.size
end

def test_middleware
Expand All @@ -43,12 +43,24 @@ def test_middleware
c.middleware.use middleware_class.new
end

assert_equal 3, Telebugs.config.middleware.middlewares.size
assert_equal 4, Telebugs.config.middleware.middlewares.size
end

def test_default_middleware_list
middleware_list = Telebugs.config.middleware.middlewares.map(&:class)
assert_includes middleware_list, Telebugs::Middleware::GemRootFilter
assert_includes middleware_list, Telebugs::Middleware::RootDirectoryFilter
end

def test_environment
Telebugs.configure { |c| c.environment = "production" }

assert_equal "production", Telebugs.config.environment
end

def test_ignore_environments
Telebugs.configure { |c| c.ignore_environments = ["test"] }

assert_equal ["test"], Telebugs.config.ignore_environments
end
end
4 changes: 2 additions & 2 deletions test/test_middleware_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_weight
stack.use TestStartLineMiddleware.new

assert_equal(
[TestStartLineMiddleware, TestFilteringMiddleware],
[TestFilteringMiddleware, TestStartLineMiddleware],
stack.middlewares.map(&:class)
)

Expand All @@ -50,7 +50,7 @@ def test_weight
stack.use TestFilteringMiddleware.new

assert_equal(
[TestStartLineMiddleware, TestFilteringMiddleware],
[TestFilteringMiddleware, TestStartLineMiddleware],
stack.middlewares.map(&:class)
)
end
Expand Down
23 changes: 21 additions & 2 deletions test/test_telebugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,44 @@ def test_configure_configures_project_key
end

def test_report_returns_a_fullfilled_promise_when_request_succeeds
stub_request(:post, Telebugs.config.api_url)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 201, body: {id: "123"}.to_json)

p = Telebugs.report(StandardError.new)
p.wait

assert p.fulfilled?
assert_requested stub
end

def test_report_returns_a_rejected_promise_when_request_fails
stub_request(:post, Telebugs.config.api_url).to_return(status: 500)
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 500, body: '{"error": "Internal Server Error"}')

p = Telebugs.report(StandardError.new)
p.wait

assert p.rejected?
assert_requested stub
end

def test_config_exposes_the_default_config
assert_match(/telebugs\.com/, Telebugs.config.api_url.to_s)
end

def test_does_not_report_errors_ignored_by_environment
stub = stub_request(:post, Telebugs.config.api_url)
.to_return(status: 201, body: {id: "123"}.to_json)

Telebugs.configure do |config|
config.environment = "test"
config.ignore_environments = %w[test]
end

p = Telebugs.report(StandardError.new)
p.wait

assert p.fulfilled?
refute_requested stub
end
end