From 6c82a50903323ac451c96264311eb07224079ce6 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 1 Dec 2017 16:23:21 +1100 Subject: [PATCH] Improve error handling in hijacked code --- lib/discourse.rb | 21 ++++++++++++++++++++- lib/hijack.rb | 6 +++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index 73698fe6182..18231f3d0a3 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -442,6 +442,25 @@ module Discourse nil 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 return if GlobalSetting.connection_reaper_age < 1 || GlobalSetting.connection_reaper_interval < 1 @@ -453,7 +472,7 @@ module Discourse sleep GlobalSetting.connection_reaper_interval reap_connections(GlobalSetting.connection_reaper_age, GlobalSetting.connection_reaper_max_age) rescue => e - Discourse.handle_exception(e, message: "Error reaping connections") + Discourse.warn_exception(e, message: "Error reaping connections") end end end diff --git a/lib/hijack.rb b/lib/hijack.rb index ab512689805..c2722e957a4 100644 --- a/lib/hijack.rb +++ b/lib/hijack.rb @@ -26,6 +26,7 @@ module Hijack MethodProfiler.start(transfer_timings) if defined? MethodProfiler begin + Thread.current[Logster::Logger::LOGSTER_ENV] = env_copy # do this first to confirm we have a working connection # before doing any work io.write "HTTP/1.1 " @@ -41,7 +42,8 @@ module Hijack begin instance.instance_eval(&blk) 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 unless instance.response_body || response.committed? @@ -71,6 +73,8 @@ module Hijack io = nil ensure + Thread.current[Logster::Logger::LOGSTER_ENV] = nil + io.close if io rescue nil if request_tracker