Improve error handling in hijacked code

This commit is contained in:
Sam 2017-12-01 16:23:21 +11:00
parent 7f2eeaf767
commit 6c82a50903
2 changed files with 25 additions and 2 deletions

View File

@ -442,6 +442,25 @@ module Discourse
nil nil
end end
# report a warning maintaining backtrack for logster
def self.warn_exception(e, message: "", env: nil)
if Rails.logger.respond_to? :add_with_opts
# logster
Rails.logger.add_with_opts(
::Logger::Severity::WARN,
"#{message} : #{e}",
"discourse-exception",
backtrace: e.backtrace.join("\n"),
env: env
)
else
# no logster ... fallback
Rails.logger.warn("#{message} #{e}")
end
rescue
STDERR.puts "Failed to report exception #{e} #{message}"
end
def self.start_connection_reaper def self.start_connection_reaper
return if GlobalSetting.connection_reaper_age < 1 || return if GlobalSetting.connection_reaper_age < 1 ||
GlobalSetting.connection_reaper_interval < 1 GlobalSetting.connection_reaper_interval < 1
@ -453,7 +472,7 @@ module Discourse
sleep GlobalSetting.connection_reaper_interval sleep GlobalSetting.connection_reaper_interval
reap_connections(GlobalSetting.connection_reaper_age, GlobalSetting.connection_reaper_max_age) reap_connections(GlobalSetting.connection_reaper_age, GlobalSetting.connection_reaper_max_age)
rescue => e rescue => e
Discourse.handle_exception(e, message: "Error reaping connections") Discourse.warn_exception(e, message: "Error reaping connections")
end end
end end
end end

View File

@ -26,6 +26,7 @@ module Hijack
MethodProfiler.start(transfer_timings) if defined? MethodProfiler MethodProfiler.start(transfer_timings) if defined? MethodProfiler
begin begin
Thread.current[Logster::Logger::LOGSTER_ENV] = env_copy
# do this first to confirm we have a working connection # do this first to confirm we have a working connection
# before doing any work # before doing any work
io.write "HTTP/1.1 " io.write "HTTP/1.1 "
@ -41,7 +42,8 @@ module Hijack
begin begin
instance.instance_eval(&blk) instance.instance_eval(&blk)
rescue => e rescue => e
Rails.logger.warn("Failed to process hijacked response correctly #{e}") # TODO we need to reuse our exception handling in ApplicationController
Discourse.warn_exception(e, message: "Failed to process hijacked response correctly", env: env_copy)
end end
unless instance.response_body || response.committed? unless instance.response_body || response.committed?
@ -71,6 +73,8 @@ module Hijack
io = nil io = nil
ensure ensure
Thread.current[Logster::Logger::LOGSTER_ENV] = nil
io.close if io rescue nil io.close if io rescue nil
if request_tracker if request_tracker