Skip to content
Open
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
15 changes: 10 additions & 5 deletions lib/hal_api/controller/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions test/hal_api/controller/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down