require "catchex/version" module CatchEx require 'socket' require "net/http" require "rake" @@CATCH_EX_URL = "http://www.catchex.com" @@DATA_PUSH_URL = @@CATCH_EX_URL + "/ex" @@PLUGIN_VERSION = "1.0" @@LANGUAGE = "Ruby" class Config @@project_key = "" @@scenario = "" @@excepted = [ActionController::RoutingError] def self.project_key(project_key = nil) unless project_key.nil? @@project_key = project_key else return @@project_key end end def self.scenario(scenario = nil) unless scenario.nil? @@scenario = scenario else return @@scenario end end end class << self def configure yield Config end # Fetch the local IP address for the hosts def local_ip orig = Socket.do_not_reverse_lookup Socket.do_not_reverse_lookup =true # turn off reverse DNS resolution temporarily UDPSocket.open do |s| s.connect 'www.catchex.com', 1 s.addr.last end ensure Socket.do_not_reverse_lookup = orig end # The catch_ex is a convenient method for posting exception. def catch(exception, from = "", url = "", params = {}) return if Config.project_key.blank? || Config.scenario.blank? || @@excepted.include?(exception.class) begin Net::HTTP.post_form(URI.parse(@@DATA_PUSH_URL), {:version => @@PLUGIN_VERSION, :language => @@LANGUAGE, :project_key => Config.project_key, :local_ip => local_ip, :message => exception.message, :scenario => Config.scenario, :backtrace => exception.backtrace.join("\n"), :from => from, :url => url, :params => params.inspect}) rescue end end end module ActionControllerExt def self.included(base) #:nodoc: base.send :include, InstanceMethods end module InstanceMethods def rescue_action(exception) catch_ex(exception) super end # Instance method for posting the customized exception. def catch_ex(exception) CatchEx.catch(exception, request.remote_ip, request.url, params) if ENV['RAILS_ENV'] == 'production' end end end module ActionViewExt def catchex_js javascript_include_tag URI.encode("http://www.catchex.com/apps/ce?porject_key=#{Config.prject_key}&scenario=#{Config.scenario}") end end end ActionController::Base.send :include, CatchEx::ActionControllerExt if defined? ActionController::Base ActionView::Base.send :include, CatchEx::ActionViewExt if defined? ActionView::Base