lukeredpath / hoptoad_notifier forked from thoughtbot/hoptoad_notifier

Reports exceptions to Hoptoad

This URL has Read+Write access

Luke Redpath (author)
Tue Sep 02 02:45:08 -0700 2008
commit  22541e33a4cbbbe501e435604b645525ec775bec
tree    f72b4e83516d5adea91295d3c1422cd7799fff49
parent  a66218b45cc8c6281e9490e30c8800951658b3b3
hoptoad_notifier / lib / hoptoad.rb
100644 73 lines (60 sloc) 2.112 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'hoptoad/catcher'
require 'hoptoad/config'
 
module Hoptoad
  
  # rails default ignores
  IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
                    'ActionController::RoutingError',
                    'ActionController::InvalidAuthenticityToken',
                    'CGI::Session::CookieStore::TamperedWithCookie']
  IGNORE_DEFAULT.map!{|e| eval(e) rescue nil }.compact!
  
  class << self
    # Call this method to modify defaults in your initializers.
    def configure
      yield config
    end
    
    def config
      @config ||= default_config
    end
    
    def reset_config
      @config = default_config
    end
    
    # You can send an exception manually using this method, even when you are not in a
    # controller. You can pass an exception or a hash that contains the attributes that
    # would be sent to Hoptoad:
    # * api_key: The API key for this project. The API key is a unique identifier that Hoptoad
    # uses for identification.
    # * error_message: The error returned by the exception (or the message you want to log).
    # * backtrace: A backtrace, usually obtained with +caller+.
    # * request: The controller's request object.
    # * session: The contents of the user's session.
    # * environment: ENV merged with the contents of the request's environment.
    def notify(notice = {})
      Sender.new.notify_hoptoad(notice)
    end
    
    private
      def default_config
        returning Hoptoad::Config.new do |config|
          IGNORE_DEFAULT.each { |exception| config.ignore << exception }
        end
      end
  end
 
  config.instance_eval do
    filter_backtrace do |line|
      line.gsub(/#{RAILS_ROOT}/, "[RAILS_ROOT]")
    end
 
    filter_backtrace do |line|
      line.gsub(/^\.\//, "")
    end
 
    filter_backtrace do |line|
      Gem.path.inject(line) do |line, path|
        line.gsub(/#{path}/, "[GEM_ROOT]")
      end
    end
  end
  
  # A dummy class for sending notifications manually outside of a controller.
  class Sender
    def rescue_action_in_public(exception)
    end
 
    include Hoptoad::Catcher
  end
end