Skip to content

Commit a96220f

Browse files
committed
Test ActionDispatch::DebugExceptions extension and fix couple deprecations
1 parent 93fed9e commit a96220f

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ class DebugExceptions
66
private
77

88
undef_method :log_error
9-
def log_error(_request, wrapper)
10-
ActiveSupport::Deprecation.silence do
11-
ActionController::Base.logger.fatal(wrapper.exception)
9+
if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7
10+
def log_error(_request, wrapper)
11+
Rails.application.deprecators.silence do
12+
ActionController::Base.logger.fatal(wrapper.exception)
13+
end
14+
end
15+
else
16+
def log_error(_request, wrapper)
17+
ActiveSupport::Deprecation.silence do
18+
ActionController::Base.logger.fatal(wrapper.exception)
19+
end
1220
end
1321
end
1422
end

test/controllers/articles_controller_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,24 @@ class ArticlesControllerTest < ActionDispatch::IntegrationTest
8484
)
8585
end
8686
end
87+
88+
describe "#show" do
89+
it "raises and logs exception" do
90+
# we're testing ActionDispatch::DebugExceptions in fact
91+
messages = semantic_logger_events do
92+
old_show = Rails.application.env_config["action_dispatch.show_exceptions"]
93+
begin
94+
Rails.application.env_config["action_dispatch.show_exceptions"] = :all
95+
get article_url(:show)
96+
rescue ActiveRecord::RecordNotFound => e
97+
# expected
98+
ensure
99+
Rails.application.env_config["action_dispatch.show_exceptions"] = old_show
100+
end
101+
end
102+
assert_equal 4, messages.count, messages
103+
assert_kind_of ActiveRecord::RecordNotFound, messages[3].exception
104+
end
105+
end
87106
end
88107
end

test/dummy/app/controllers/articles_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ def new
55
def create
66
render plain: params[:article].inspect
77
end
8+
9+
def show
10+
raise ActiveRecord::RecordNotFound
11+
end
812
end

test/dummy/config/application.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
module Dummy
88
class Application < Rails::Application
9+
if config.respond_to?(:load_defaults)
10+
config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}"
11+
end
12+
913
# Configure sensitive parameters which will be filtered from the log file.
1014
config.filter_parameters += [:password]
1115
config.active_record.sqlite3.represent_boolean_as_integer = true if config.active_record.sqlite3

test/dummy/config/environments/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
config.action_controller.perform_caching = false
2020

2121
# Raise exceptions instead of rendering exception templates
22-
config.action_dispatch.show_exceptions = false
22+
config.action_dispatch.show_exceptions = Rails::VERSION::MAJOR >= 7 ? :none : false
2323

2424
# Disable request forgery protection in test environment
2525
config.action_controller.allow_forgery_protection = false

0 commit comments

Comments
 (0)