Skip to content

Instantly share code, notes, and snippets.

@tedgrubb
Created July 16, 2013 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedgrubb/6010687 to your computer and use it in GitHub Desktop.
Save tedgrubb/6010687 to your computer and use it in GitHub Desktop.
ActiveResource LogSubscriber exception logging instead of breaking on nil result. Fixes undefined method 'code' for NilClass when ActiveResource throws a TimeoutError.
module ActiveResource
class LogSubscriber < ActiveSupport::LogSubscriber
def request(event)
result = event.payload[:result]
info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}"
if event.payload.has_key?(:exception)
error "#{event.payload[:exception].join(' ')}\n"
else
info "STATUS %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration]
info "BODY #{result.body}\n"
end
end
def logger
ActiveResource::Base.logger
end
end
end
# This is already called in /active_resource/log_subscriber.rb
# ActiveResource::LogSubscriber.attach_to :active_resource
# Attach to STDOUT
ActiveResource::Base.logger = Logger.new(STDOUT)
ActiveResource::Base.logger.level = Logger::DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment