Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Separating Rails functionality out more

  • Loading branch information...
commit a4dcaf59ad4b620adf2cce9b34d3c5728466c123 1 parent 59a9be6
@jferris jferris authored
View
2  features/step_definitions/rails_application_steps.rb
@@ -89,7 +89,7 @@
Then /^I should see "([^\"]*)"$/ do |expected_text|
unless @terminal.output.include?(expected_text)
- raise "Got terminal output:\n#{@terminal.output}\nExpected output:\n#{expected_text}"
+ raise("Got terminal output:\n#{@terminal.output}\n\nExpected output:\n#{expected_text}")
end
end
View
3  lib/hoptoad_notifier.rb
@@ -6,7 +6,6 @@
require 'hoptoad_notifier/configuration'
require 'hoptoad_notifier/notice'
require 'hoptoad_notifier/sender'
-require 'hoptoad_notifier/catcher'
require 'hoptoad_notifier/backtrace'
# Gem for applications to automatically post errors to the Hoptoad of their choice.
@@ -47,7 +46,7 @@ def report_response_body(response)
# Returns the Ruby version, Rails version, and current Rails environment
def environment_info
info = "[Ruby: #{RUBY_VERSION}]"
- info << " [Rails: #{::Rails::VERSION::STRING}]" if defined?(Rails)
+ # info << " [Rails: #{::Rails::VERSION::STRING}]" if defined?(Rails)
info << " [Env: #{configuration.environment_name}]"
end
View
95 lib/hoptoad_notifier/catcher.rb
@@ -1,95 +0,0 @@
-module HoptoadNotifier
- # Include this module in Controllers in which you want to be notified of errors.
- module Catcher
-
- # Sets up an alias chain to catch exceptions when Rails does
- def self.included(base) #:nodoc:
- if base.instance_methods.map(&:to_s).include? 'rescue_action_in_public' and !base.instance_methods.map(&:to_s).include? 'rescue_action_in_public_without_hoptoad'
- base.send(:alias_method, :rescue_action_in_public_without_hoptoad, :rescue_action_in_public)
- base.send(:alias_method, :rescue_action_in_public, :rescue_action_in_public_with_hoptoad)
- base.send(:alias_method, :rescue_action_locally_without_hoptoad, :rescue_action_locally)
- base.send(:alias_method, :rescue_action_locally, :rescue_action_locally_with_hoptoad)
- end
- end
-
- private
-
- # Overrides the rescue_action method in ActionController::Base, but does not inhibit
- # any custom processing that is defined with Rails 2's exception helpers.
- def rescue_action_in_public_with_hoptoad(exception)
- unless hoptoad_ignore_user_agent?
- HoptoadNotifier.notify_or_ignore(exception, hoptoad_request_data)
- end
- rescue_action_in_public_without_hoptoad(exception)
- end
-
- def rescue_action_locally_with_hoptoad(exception)
- result = rescue_action_locally_without_hoptoad(exception)
-
- if HoptoadNotifier.configuration.development_lookup
- path = File.join(File.dirname(__FILE__), '..', 'templates', 'rescue.erb')
- notice = HoptoadNotifier.build_lookup_hash_for(exception, hoptoad_request_data)
-
- result << @template.render(
- :file => path,
- :use_full_path => false,
- :locals => { :host => HoptoadNotifier.configuration.host,
- :api_key => HoptoadNotifier.configuration.api_key,
- :notice => notice })
- end
-
- result
- end
-
- # This method should be used for sending manual notifications while you are still
- # inside the controller. Otherwise it works like HoptoadNotifier.notify.
- def notify_hoptoad(hash_or_exception)
- unless consider_all_requests_local || local_request?
- HoptoadNotifier.notify(hash_or_exception, hoptoad_request_data)
- end
- end
-
- def hoptoad_ignore_user_agent? #:nodoc:
- # Rails 1.2.6 doesn't have request.user_agent, so check for it here
- user_agent = request.respond_to?(:user_agent) ? request.user_agent : request.env["HTTP_USER_AGENT"]
- HoptoadNotifier.configuration.ignore_user_agent.flatten.any? { |ua| ua === user_agent }
- end
-
- def hoptoad_request_data
- { :parameters => hoptoad_filter_if_filtering(params.to_hash),
- :session_data => hoptoad_session_data,
- :controller => params[:controller],
- :action => params[:action],
- :url => hoptoad_request_url,
- :cgi_data => hoptoad_filter_if_filtering(request.env),
- :environment_vars => hoptoad_filter_if_filtering(ENV) }
- end
-
- def hoptoad_filter_if_filtering(hash)
- if respond_to?(:filter_parameters)
- filter_parameters(hash) rescue hash
- else
- hash
- end
- end
-
- def hoptoad_session_data
- if session.respond_to?(:to_hash)
- session.to_hash
- else
- session.data
- end
- end
-
- def hoptoad_request_url
- url = "#{request.protocol}#{request.host}"
-
- unless [80, 443].include?(request.port)
- url << ":#{request.port}"
- end
-
- url << request.request_uri
- url
- end
- end
-end
View
36 lib/hoptoad_notifier/rails.rb
@@ -1,11 +1,31 @@
-if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher)
- ActionController::Base.send(:include, HoptoadNotifier::Catcher)
-end
+require 'hoptoad_notifier'
+require 'hoptoad_notifier/rails/controller_methods'
+require 'hoptoad_notifier/rails/action_controller_catcher'
+require 'hoptoad_notifier/rails/error_lookup'
+
+module HoptoadNotifier
+ module Rails
+ def self.initialize
+ if defined?(ActionController::Base)
+ ActionController::Base.send(:include, HoptoadNotifier::Rails::ActionControllerCatcher)
+ ActionController::Base.send(:include, HoptoadNotifier::Rails::ErrorLookup)
+ ActionController::Base.send(:include, HoptoadNotifier::Rails::ControllerMethods)
+ end
-require 'hoptoad_notifier/rails_initializer'
-HoptoadNotifier::RailsInitializer.initialize
+ rails_logger = if defined?(::Rails.logger)
+ ::Rails.logger
+ elsif defined?(RAILS_DEFAULT_LOGGER)
+ RAILS_DEFAULT_LOGGER
+ end
-HoptoadNotifier.configure(true) do |config|
- config.environment_name = RAILS_ENV
- config.project_root = RAILS_ROOT
+ HoptoadNotifier.configure(true) do |config|
+ config.logger = rails_logger
+ config.environment_name = RAILS_ENV if defined?(RAILS_ENV)
+ config.project_root = RAILS_ROOT if defined?(RAILS_ROOT)
+ end
+ end
+ end
end
+
+HoptoadNotifier::Rails.initialize
+
View
29 lib/hoptoad_notifier/rails/action_controller_catcher.rb
@@ -0,0 +1,29 @@
+module HoptoadNotifier
+ module Rails
+ module ActionControllerCatcher
+
+ # Sets up an alias chain to catch exceptions when Rails does
+ def self.included(base) #:nodoc:
+ base.send(:alias_method, :rescue_action_in_public_without_hoptoad, :rescue_action_in_public)
+ base.send(:alias_method, :rescue_action_in_public, :rescue_action_in_public_with_hoptoad)
+ end
+
+ private
+
+ # Overrides the rescue_action method in ActionController::Base, but does not inhibit
+ # any custom processing that is defined with Rails 2's exception helpers.
+ def rescue_action_in_public_with_hoptoad(exception)
+ unless hoptoad_ignore_user_agent?
+ HoptoadNotifier.notify_or_ignore(exception, hoptoad_request_data)
+ end
+ rescue_action_in_public_without_hoptoad(exception)
+ end
+
+ def hoptoad_ignore_user_agent? #:nodoc:
+ # Rails 1.2.6 doesn't have request.user_agent, so check for it here
+ user_agent = request.respond_to?(:user_agent) ? request.user_agent : request.env["HTTP_USER_AGENT"]
+ HoptoadNotifier.configuration.ignore_user_agent.flatten.any? { |ua| ua === user_agent }
+ end
+ end
+ end
+end
View
59 lib/hoptoad_notifier/rails/controller_methods.rb
@@ -0,0 +1,59 @@
+module HoptoadNotifier
+ module Rails
+ module ControllerMethods
+ private
+
+ # This method should be used for sending manual notifications while you are still
+ # inside the controller. Otherwise it works like HoptoadNotifier.notify.
+ def notify_hoptoad(hash_or_exception)
+ unless consider_all_requests_local || local_request?
+ HoptoadNotifier.notify(hash_or_exception, hoptoad_request_data)
+ end
+ end
+
+ def hoptoad_ignore_user_agent? #:nodoc:
+ # Rails 1.2.6 doesn't have request.user_agent, so check for it here
+ user_agent = request.respond_to?(:user_agent) ? request.user_agent : request.env["HTTP_USER_AGENT"]
+ HoptoadNotifier.configuration.ignore_user_agent.flatten.any? { |ua| ua === user_agent }
+ end
+
+ def hoptoad_request_data
+ { :parameters => hoptoad_filter_if_filtering(params.to_hash),
+ :session_data => hoptoad_session_data,
+ :controller => params[:controller],
+ :action => params[:action],
+ :url => hoptoad_request_url,
+ :cgi_data => hoptoad_filter_if_filtering(request.env),
+ :environment_vars => hoptoad_filter_if_filtering(ENV) }
+ end
+
+ def hoptoad_filter_if_filtering(hash)
+ if respond_to?(:filter_parameters)
+ filter_parameters(hash) rescue hash
+ else
+ hash
+ end
+ end
+
+ def hoptoad_session_data
+ if session.respond_to?(:to_hash)
+ session.to_hash
+ else
+ session.data
+ end
+ end
+
+ def hoptoad_request_url
+ url = "#{request.protocol}#{request.host}"
+
+ unless [80, 443].include?(request.port)
+ url << ":#{request.port}"
+ end
+
+ url << request.request_uri
+ url
+ end
+ end
+ end
+end
+
View
33 lib/hoptoad_notifier/rails/error_lookup.rb
@@ -0,0 +1,33 @@
+module HoptoadNotifier
+ module Rails
+ module ErrorLookup
+
+ # Sets up an alias chain to catch exceptions when Rails does
+ def self.included(base) #:nodoc:
+ base.send(:alias_method, :rescue_action_locally_without_hoptoad, :rescue_action_locally)
+ base.send(:alias_method, :rescue_action_locally, :rescue_action_locally_with_hoptoad)
+ end
+
+ private
+
+ def rescue_action_locally_with_hoptoad(exception)
+ result = rescue_action_locally_without_hoptoad(exception)
+
+ if HoptoadNotifier.configuration.development_lookup
+ path = File.join(File.dirname(__FILE__), '..', '..', 'templates', 'rescue.erb')
+ notice = HoptoadNotifier.build_lookup_hash_for(exception, hoptoad_request_data)
+
+ result << @template.render(
+ :file => path,
+ :use_full_path => false,
+ :locals => { :host => HoptoadNotifier.configuration.host,
+ :api_key => HoptoadNotifier.configuration.api_key,
+ :notice => notice })
+ end
+
+ result
+ end
+ end
+ end
+end
+
View
16 lib/hoptoad_notifier/rails_initializer.rb
@@ -1,16 +0,0 @@
-module HoptoadNotifier
- # used to initialize Rails-specific code
- class RailsInitializer
- def self.initialize
- rails_logger = if defined?(Rails.logger)
- Rails.logger
- elsif defined?(RAILS_DEFAULT_LOGGER)
- RAILS_DEFAULT_LOGGER
- end
-
- HoptoadNotifier.configure(true) do |config|
- config.logger = rails_logger if config.respond_to? :logger
- end
- end
- end
-end
View
7 lib/hoptoad_notifier/tasks.rb
@@ -35,10 +35,11 @@ class HoptoadTestingException < RuntimeError; end
HoptoadNotifier.configuration.development_environments = []
- in_controller = ApplicationController.included_modules.include? HoptoadNotifier::Catcher
- in_base = ActionController::Base.included_modules.include? HoptoadNotifier::Catcher
+ catcher = HoptoadNotifier::Rails::ActionControllerCatcher
+ in_controller = ApplicationController.included_modules.include?(catcher)
+ in_base = ActionController::Base.included_modules.include?(catcher)
if !in_controller || !in_base
- puts "HoptoadNotifier::Catcher must be included inside your ApplicationController class."
+ puts "Rails initialization did not occur"
exit
end
View
6 test/catcher_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
-class CatcherTest < Test::Unit::TestCase
+class ActionControllerCatcherTest < Test::Unit::TestCase
include DefinesConstants
@@ -17,7 +17,7 @@ def ignore(exception_class)
def build_controller_class(&definition)
returning Class.new(ActionController::Base) do |klass|
- klass.__send__(:include, HoptoadNotifier::Catcher)
+ klass.__send__(:include, HoptoadNotifier::Rails::ActionControllerCatcher)
klass.class_eval(&definition) if definition
define_constant('HoptoadTestController', klass)
end
@@ -172,7 +172,7 @@ def process_action_with_automatic_notification(args = {})
should "not create actions from Hoptoad methods" do
controller = build_controller_class.new
- assert_equal [], HoptoadNotifier::Catcher.instance_methods
+ assert_equal [], HoptoadNotifier::Rails::ActionControllerCatcher.instance_methods
end
should "ignore exceptions when user agent is being ignored by regular expression" do
View
7 test/helper.rb
@@ -3,10 +3,12 @@
gem 'jferris-mocha', '>= 0.9.5.0.1241126838'
+$LOAD_PATH << File.join(File.dirname(__FILE__), *%w[.. vendor ginger lib])
+$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
+
require 'shoulda'
require 'mocha'
-$LOAD_PATH << File.join(File.dirname(__FILE__), *%w[.. vendor ginger lib])
require 'ginger'
require 'action_controller'
@@ -16,7 +18,7 @@
require 'active_support'
require 'nokogiri'
-require File.join(File.dirname(__FILE__), "..", "lib", "hoptoad_notifier")
+require "hoptoad_notifier"
begin require 'redgreen'; rescue LoadError; end
@@ -234,4 +236,3 @@ def error(*args); end
def fatal(*args); end
end
-RAILS_DEFAULT_LOGGER = FakeLogger.new
View
9 test/rails_initializer_test.rb
@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/helper'
-require File.join(File.dirname(__FILE__), '..', 'lib', 'hoptoad_notifier', 'rails_initializer')
+
+require 'hoptoad_notifier/rails'
class RailsInitializerTest < Test::Unit::TestCase
include DefinesConstants
@@ -11,20 +12,20 @@ def self.logger
end
end
define_constant("Rails", rails)
- HoptoadNotifier::RailsInitializer.initialize
+ HoptoadNotifier::Rails.initialize
assert_equal "RAILS LOGGER", HoptoadNotifier.logger
end
should "trigger use of Rails' default logger if logger isn't set and Rails.logger doesn't exist" do
define_constant("RAILS_DEFAULT_LOGGER", "RAILS DEFAULT LOGGER")
- HoptoadNotifier::RailsInitializer.initialize
+ HoptoadNotifier::Rails.initialize
assert_equal "RAILS DEFAULT LOGGER", HoptoadNotifier.logger
end
should "allow overriding of the logger if already assigned" do
define_constant("RAILS_DEFAULT_LOGGER", "RAILS DEFAULT LOGGER")
- HoptoadNotifier::RailsInitializer.initialize
+ HoptoadNotifier::Rails.initialize
HoptoadNotifier.configure(true) do |config|
config.logger = "OVERRIDDEN LOGGER"
Please sign in to comment.
Something went wrong with that request. Please try again.