diff --git a/lib/hal_api/controller/exceptions.rb b/lib/hal_api/controller/exceptions.rb index cdfa14c..ecec752 100644 --- a/lib/hal_api/controller/exceptions.rb +++ b/lib/hal_api/controller/exceptions.rb @@ -47,12 +47,17 @@ def log_error(env, wrapper) trace = wrapper.framework_trace if trace.empty? message = "\n#{exception.class} (#{exception.message}):\n" - if exception.respond_to?(:annoted_source_code) - ActiveSupport::Deprecation.silence do - message << exception.annoted_source_code.to_s - end + silencer = if ActiveSupport::Deprecation.respond_to?(:silence) + ActiveSupport::Deprecation + else + # rails 7 change to use instance of depracator + ActiveSupport::Deprecation.new + end + silencer.silence do + message.concat(exception.annoted_source_code) if exception.respond_to?(:annoted_source_code) + # rails 6 change annoted_source_code -> annotated_source_code + message.concat(exception.annotated_source_code) if exception.respond_to?(:annotated_source_code) end - message << " " << trace.join("\n ") logger.fatal("#{message}\n\n") end diff --git a/test/hal_api/controller/exceptions_test.rb b/test/hal_api/controller/exceptions_test.rb index a0fcdb5..4b2b77f 100644 --- a/test/hal_api/controller/exceptions_test.rb +++ b/test/hal_api/controller/exceptions_test.rb @@ -17,6 +17,22 @@ def throwerror def thrownotfound raise HalApi::Errors::NotFound.new end + + def logger + @logger ||= Logger.new(logged) + end + + def logged + @_logged ||= StringIO.new + end + end + + class AnnotatedError < StandardError + def annoted_source_code + "121: str.to_s" + end + + alias_method :annotated_source_code, :annoted_source_code end before do @@ -48,6 +64,22 @@ def thrownotfound _(json['backtrace']).must_be_instance_of Array end + it 'logs the source code silently' do + cleaner = ::ActiveSupport::BacktraceCleaner.new + exception = AnnotatedError.new("did you hear something?") + wrapper = + if HalApi.rails_major_version >= 5 + ::ActionDispatch::ExceptionWrapper.new(cleaner, exception) + else + ::ActionDispatch::ExceptionWrapper.new(:test, exception) + end + + controller = ExceptionsTestController.new + + controller.log_error(:test, wrapper) + _(controller.logged.string).must_match(/FATAL.*AnnotatedError.*121: str\.to_s/m) + end + it 'does not try to set a location header for post errors' do post '/throwerror.hal' _(response.status).must_equal 500