Skip to content

Commit

Permalink
Added hoptoad_notifier
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/hoptoad_notifier/trunk@272 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jyurek committed Dec 18, 2007
0 parents commit 34a3106
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HoptoadNotifier
===============

This is the notifier plugin for integrating apps with Hoptoad. When an
uncaught exception occurs, HoptoadNotifier will POST the relevant data
to the Hoptoad server specified in your environment.rb

HoptoadNotifier.url = "http://192.168.1.33:3000/notices/"
HoptoadNotifier.app_name = "my_sweet_app"

Then make sure you have

include HoptoadNotifier::Catcher

in your ApplicationController, and all exceptions will be logged to Hoptoad
where they can be aggregated, filtered, and searched.
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the hoptoad_notifier plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the hoptoad_notifier plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'HoptoadNotifier'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
92 changes: 92 additions & 0 deletions lib/hoptoad_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Plugin for applications to automatically post errors to Hoptoad.
module HoptoadNotifier

def self.url; @url; end
def self.url= url; @url = URI.parse(url); end

def self.app_name; @app_name; end
def self.app_name= app_name; @app_name = app_name; end

module Catcher
def self.included target
target.send( :include, Handlers )
end

module Handlers

private

def rescue_action_in_public exception
if is_a_404?(exception)
render_not_found_page
else
render_error_page
data = {
'project_name' => HoptoadNotifier.app_name,
'error_message' => exception.message,
'backtrace' => exception.backtrace.to_json,
'request' => {
'params' => request.parameters.to_hash,
'rails_root' => File.expand_path(RAILS_ROOT),
'url' => "#{request.protocol}#{request.host}#{request.request_uri}"
}.to_json,
'session' => {
'key' => session.instance_variable_get("@session_id"),
'data' => session.instance_variable_get("@data")
}.to_json,
'environment' => ENV.to_hash.to_json
}
inform_hoptoad(data)
end
end

def render_not_found_page
respond_to do |wants|
wants.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => :not_found }
wants.all { render :nothing => true, :status => :not_found }
end
end

def render_error_page
respond_to do |wants|
wants.html { render :file => "#{RAILS_ROOT}/public/500.html", :status => :internal_server_error }
wants.all { render :nothing => true, :status => :internal_server_error }
end
end

def inform_hoptoad data
url = HoptoadNotifier.url
Net::HTTP.start(url.host, url.port) do |http|
response = http.post(url.path, to_params(data), {'Accept', 'text/xml, application/xml'})
case response
when Net::HTTPSuccess then
logger.info "Hoptoad Success: #{response.class}"
when Net::HTTPRedirect then
logger.info "Hoptoad Success: #{response.class}"
else
logger.error "Hoptoad Failure: #{response.class}"
end
end
end

def is_a_404? exception
[
ActiveRecord::RecordNotFound,
ActionController::UnknownController,
ActionController::UnknownAction
].include?( exception )
end

def to_params thing, context = "notice"
case thing
when Hash
thing.map{|key, val| to_params(val, "#{context}[#{key}]") }.join("&")
when Array
thing.map{|val| to_params(val, "#{context}[]") }.join("&")
else
"#{CGI.escape(context)}=#{CGI.escape(thing.to_s)}"
end
end
end
end
end
4 changes: 4 additions & 0 deletions tasks/hoptoad_notifier_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :hoptoad_notifier do
# # Task goes here
# end
5 changes: 5 additions & 0 deletions test/hoptoad_notifier_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'test/unit'

class HoptoadNotifierTest < Test::Unit::TestCase

end

0 comments on commit 34a3106

Please sign in to comment.