From 66c04baa8044dd696be76c75af4c4af8deace3f2 Mon Sep 17 00:00:00 2001 From: jsaak Date: Fri, 29 Nov 2013 10:10:50 +0100 Subject: [PATCH 1/2] added log_exception to env --- lib/goliath/api.rb | 6 +----- lib/goliath/env.rb | 11 +++++++++++ lib/goliath/rack/validator.rb | 3 +-- lib/goliath/request.rb | 2 +- lib/goliath/websocket.rb | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/goliath/api.rb b/lib/goliath/api.rb index f71ed604..db85165d 100644 --- a/lib/goliath/api.rb +++ b/lib/goliath/api.rb @@ -175,11 +175,7 @@ def call(env) env[ASYNC_CALLBACK].call(validation_error(e.status_code, e.message)) rescue Exception => e - logthis = "#{e.backtrace[0]}: #{e.message} (#{e.class})\n" - e.backtrace[1..-1].each do |bt| - logthis += " from #{bt}\n" - end - env.logger.error(logthis) + env.log_exception(e) env[RACK_EXCEPTION] = e message = Goliath.env?(:production) ? 'An error happened' : e.message diff --git a/lib/goliath/env.rb b/lib/goliath/env.rb index 2539a132..8c92d852 100644 --- a/lib/goliath/env.rb +++ b/lib/goliath/env.rb @@ -113,6 +113,17 @@ def logger self[RACK_LOGGER] end + # Convenience method for logging exceptions + # + # @return [Logger] The logger object + def log_exception(e) + logthis = "Exception:\n#{e.backtrace[0].gsub(/(.*?:.*?):(.*)/,'\1 \2')}: #{e.message} (#{e.class})\n" + e.backtrace[1..-1].each do |bt| + logthis += "\tfrom #{bt.gsub(/(.*?:.*?):(.*)/,'\1 \2')}\n" + end + self[RACK_LOGGER].error(logthis) + end + # @param name [Symbol] The method to check if we respond to it. # @return [Boolean] True if the Env responds to the method, false otherwise def respond_to?(name) diff --git a/lib/goliath/rack/validator.rb b/lib/goliath/rack/validator.rb index b1b21809..4a1a4b9e 100644 --- a/lib/goliath/rack/validator.rb +++ b/lib/goliath/rack/validator.rb @@ -41,8 +41,7 @@ def safely(env, headers={}) rescue Goliath::Validation::Error => e validation_error(e.status_code, e.message, headers) rescue Exception => e - env.logger.error(e.message) - env.logger.error(e.backtrace.join("\n")) + env.log_exception(e) validation_error(500, e.message, headers) end end diff --git a/lib/goliath/request.rb b/lib/goliath/request.rb index 9ea2d6e9..da902b09 100644 --- a/lib/goliath/request.rb +++ b/lib/goliath/request.rb @@ -228,7 +228,7 @@ def server_exception(e) if e.is_a?(Goliath::Validation::Error) status, headers, body = [e.status_code, {}, ('{"error":"%s"}' % e.message)] else - @env[RACK_LOGGER].error("#{e.message}\n#{e.backtrace.join("\n")}") + @env.log_exception(e) message = Goliath.env?(:production) ? 'An error happened' : e.message status, headers, body = [500, {}, message] diff --git a/lib/goliath/websocket.rb b/lib/goliath/websocket.rb index 8f72edba..9cd85dcc 100644 --- a/lib/goliath/websocket.rb +++ b/lib/goliath/websocket.rb @@ -81,7 +81,7 @@ def max_frame_size env['handler'] = EM::WebSocket::HandlerFactory.build_with_request(conn, request, upgrade_data, false, false) rescue Exception => e - env.logger.error("#{e.message}\n#{e.backtrace.join("\n")}") + env.log_exception(e) return [500, {}, {:error => "Upgrade failed"}] end env['handler'].run From 90b590639c9fdafd0e3ab953d5ac380339830a72 Mon Sep 17 00:00:00 2001 From: jsaak Date: Fri, 29 Nov 2013 10:18:32 +0100 Subject: [PATCH 2/2] fixed method documentation --- lib/goliath/env.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/goliath/env.rb b/lib/goliath/env.rb index 8c92d852..523f65d0 100644 --- a/lib/goliath/env.rb +++ b/lib/goliath/env.rb @@ -115,7 +115,7 @@ def logger # Convenience method for logging exceptions # - # @return [Logger] The logger object + # @param e [Exception] The exception to log def log_exception(e) logthis = "Exception:\n#{e.backtrace[0].gsub(/(.*?:.*?):(.*)/,'\1 \2')}: #{e.message} (#{e.class})\n" e.backtrace[1..-1].each do |bt|