2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2014-11-13 17:25:33 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
class ExceptionWrapperTest < ActionDispatch::IntegrationTest
|
|
|
|
class TestError < StandardError
|
|
|
|
attr_reader :backtrace
|
|
|
|
|
|
|
|
def initialize(*backtrace)
|
|
|
|
@backtrace = backtrace.flatten
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-16 10:17:06 -05:00
|
|
|
class BadlyDefinedError < StandardError
|
|
|
|
def backtrace
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-13 17:25:33 -05:00
|
|
|
setup do
|
2015-08-06 18:59:30 -04:00
|
|
|
@cleaner = ActiveSupport::BacktraceCleaner.new
|
2018-08-05 21:06:01 -04:00
|
|
|
@cleaner.remove_filters!
|
2015-08-06 18:59:30 -04:00
|
|
|
@cleaner.add_silencer { |line| line !~ /^lib/ }
|
2014-11-13 17:25:33 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#source_extracts fetches source fragments for every backtrace entry" do
|
2014-11-13 17:25:33 -05:00
|
|
|
exception = TestError.new("lib/file.rb:42:in `index'")
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(nil, exception)
|
2014-11-13 17:25:33 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_called_with(wrapper, :source_fragment, ["lib/file.rb", 42], returns: "foo") do
|
|
|
|
assert_equal [ code: "foo", line_number: 42 ], wrapper.source_extracts
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2014-11-13 17:25:33 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#source_extracts works with Windows paths" do
|
2015-01-31 13:00:33 -05:00
|
|
|
exc = TestError.new("c:/path/to/rails/app/controller.rb:27:in 'index':")
|
|
|
|
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(nil, exc)
|
2015-01-31 13:00:33 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_called_with(wrapper, :source_fragment, ["c:/path/to/rails/app/controller.rb", 27], returns: "nothing") do
|
|
|
|
assert_equal [ code: "nothing", line_number: 27 ], wrapper.source_extracts
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2015-01-31 13:00:33 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#source_extracts works with non standard backtrace" do
|
|
|
|
exc = TestError.new("invalid")
|
2015-01-31 13:00:33 -05:00
|
|
|
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(nil, exc)
|
2015-01-31 13:00:33 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_called_with(wrapper, :source_fragment, ["invalid", 0], returns: "nothing") do
|
|
|
|
assert_equal [ code: "nothing", line_number: 0 ], wrapper.source_extracts
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2015-01-31 13:00:33 -05:00
|
|
|
end
|
2014-11-16 10:17:06 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#application_trace returns traces only from the application" do
|
2014-11-13 17:25:33 -05:00
|
|
|
exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(@cleaner, exception)
|
2014-11-13 17:25:33 -05:00
|
|
|
|
|
|
|
assert_equal [ "lib/file.rb:42:in `index'" ], wrapper.application_trace
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#status_code returns 400 for Rack::Utils::ParameterTypeError" do
|
2016-07-12 11:41:09 -04:00
|
|
|
exception = Rack::Utils::ParameterTypeError.new
|
|
|
|
wrapper = ExceptionWrapper.new(@cleaner, exception)
|
|
|
|
assert_equal 400, wrapper.status_code
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#application_trace cannot be nil" do
|
2015-08-06 18:59:30 -04:00
|
|
|
nil_backtrace_wrapper = ExceptionWrapper.new(@cleaner, BadlyDefinedError.new)
|
|
|
|
nil_cleaner_wrapper = ExceptionWrapper.new(nil, BadlyDefinedError.new)
|
2014-11-16 10:17:06 -05:00
|
|
|
|
|
|
|
assert_equal [], nil_backtrace_wrapper.application_trace
|
|
|
|
assert_equal [], nil_cleaner_wrapper.application_trace
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#framework_trace returns traces outside the application" do
|
2014-11-13 17:25:33 -05:00
|
|
|
exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(@cleaner, exception)
|
2014-11-13 17:25:33 -05:00
|
|
|
|
|
|
|
assert_equal caller, wrapper.framework_trace
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#framework_trace cannot be nil" do
|
2015-08-06 18:59:30 -04:00
|
|
|
nil_backtrace_wrapper = ExceptionWrapper.new(@cleaner, BadlyDefinedError.new)
|
|
|
|
nil_cleaner_wrapper = ExceptionWrapper.new(nil, BadlyDefinedError.new)
|
2014-11-16 10:17:06 -05:00
|
|
|
|
|
|
|
assert_equal [], nil_backtrace_wrapper.framework_trace
|
|
|
|
assert_equal [], nil_cleaner_wrapper.framework_trace
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#full_trace returns application and framework traces" do
|
2014-11-13 17:25:33 -05:00
|
|
|
exception = TestError.new(caller.prepend("lib/file.rb:42:in `index'"))
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(@cleaner, exception)
|
2014-11-13 17:25:33 -05:00
|
|
|
|
|
|
|
assert_equal exception.backtrace, wrapper.full_trace
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#full_trace cannot be nil" do
|
2015-08-06 18:59:30 -04:00
|
|
|
nil_backtrace_wrapper = ExceptionWrapper.new(@cleaner, BadlyDefinedError.new)
|
|
|
|
nil_cleaner_wrapper = ExceptionWrapper.new(nil, BadlyDefinedError.new)
|
2014-11-16 10:17:06 -05:00
|
|
|
|
|
|
|
assert_equal [], nil_backtrace_wrapper.full_trace
|
|
|
|
assert_equal [], nil_cleaner_wrapper.full_trace
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "#traces returns every trace by category enumerated with an index" do
|
2014-11-13 17:25:33 -05:00
|
|
|
exception = TestError.new("lib/file.rb:42:in `index'", "/gems/rack.rb:43:in `index'")
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(@cleaner, exception)
|
2014-11-13 17:25:33 -05:00
|
|
|
|
|
|
|
assert_equal({
|
2018-04-01 21:47:07 -04:00
|
|
|
"Application Trace" => [
|
|
|
|
exception_object_id: exception.object_id,
|
|
|
|
id: 0,
|
|
|
|
trace: "lib/file.rb:42:in `index'"
|
|
|
|
],
|
|
|
|
"Framework Trace" => [
|
|
|
|
exception_object_id: exception.object_id,
|
|
|
|
id: 1,
|
|
|
|
trace: "/gems/rack.rb:43:in `index'"
|
|
|
|
],
|
2016-08-06 12:54:50 -04:00
|
|
|
"Full Trace" => [
|
2018-04-01 21:47:07 -04:00
|
|
|
{
|
|
|
|
exception_object_id: exception.object_id,
|
|
|
|
id: 0,
|
|
|
|
trace: "lib/file.rb:42:in `index'"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exception_object_id: exception.object_id,
|
|
|
|
id: 1,
|
|
|
|
trace: "/gems/rack.rb:43:in `index'"
|
|
|
|
}
|
2014-11-13 17:25:33 -05:00
|
|
|
]
|
|
|
|
}, wrapper.traces)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|