Module: WhoopsNotifier

Defined in:
lib/merb_whoops_notifier/whoops_notifier.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 5

def host
  @host
end

.loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 5

def logger
  @logger
end

Class Method Details

.clean_non_serializable_data(notice) ⇒ Object

:nodoc:



115
116
117
118
119
120
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 115

def clean_non_serializable_data(notice) #:nodoc:
  notice.select{|k,v| serializable?(v) }.inject({}) do |h, pair|
    h[pair.first] = pair.last.is_a?(Hash) ? clean_non_serializable_data(pair.last) : pair.last
    h
  end
end

.clean_whoops_environment(env) ⇒ Object

:nodoc:



136
137
138
139
140
141
142
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 136

def clean_whoops_environment(env) #:nodoc:
  env.each do |k, v|
    env[k] = "[FILTERED]" if WhoopsNotifier.environment_filters.any? do |filter|
      k.to_s.match(/#{filter}/)
    end
  end
end

.configureObject



7
8
9
10
11
12
13
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 7

def configure
  key = YAML.load_file(Merb.root / 'config' / 'whoops.yml')
  if key
    env = key[Merb.env.to_sym]
    env ? @host = env[:host] : raise(ArgumentError, "No whoops host for Merb environment #{Merb.env}")
  end
end

.default_notice_optionsObject

:nodoc:



105
106
107
108
109
110
111
112
113
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 105

def default_notice_options #:nodoc:
  {
    :error_message => 'Notification',
    :backtrace     => nil,
    :request       => {},
    :session       => {},
    :environment   => {}
  }
end

.environment_filtersObject



19
20
21
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 19

def environment_filters
  @environment_filters ||= %w(AWS_ACCESS_KEY  AWS_SECRET_ACCESS_KEY AWS_ACCOUNT SSH_AUTH_SOCK)
end

.notify_whoops(request, session) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 50

def notify_whoops(request, session)
  return if request.nil?
  params = request.params

  request.exceptions.each do |exception|
    data = {
      :error_class   => Extlib::Inflection.camelize(exception.class.name),
      :error_message => "#{Extlib::Inflection.camelize(exception.class.name)}: #{exception.message}",
      :backtrace     => exception.backtrace,
      :environment   => ENV.to_hash
    }

    data[:request] = {
      :params => params
    }

    data[:environment] = clean_whoops_environment(ENV.to_hash.merge(request.env))
    data[:environment][:app_env] = Merb.env

    data[:session] = {
       :key         => session.instance_variable_get("@session_id"),
       :data        => session.to_hash
    }

    send_to_whoops :notice => default_notice_options.merge(data)
  end
  true
end

.send_to_whoops(data) ⇒ Object

:nodoc:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 79

def send_to_whoops(data) #:nodoc:
  url = URI.parse(WhoopsNotifier.host)

  Net::HTTP.start(url.host, url.port) do |http|
    headers = {
      'Content-type' => 'application/x-yaml',
      'Accept' => 'text/xml, application/xml'
    }
    http.read_timeout = 5 # seconds
    http.open_timeout = 2 # seconds
    # http.use_ssl = WhoopsNotifier.secure
    response = begin
                 http.post("/error_reports", clean_non_serializable_data(data).to_yaml, headers)
               rescue TimeoutError => e
                 logger.error "Timeout while contacting the Whoops server."
                 nil
               end
    case response
    when Net::HTTPSuccess then
      logger.info "Whoops Success: #{response.class}"
    else
      logger.error "Whoops Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
    end
  end            
end

.serializable?(value) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


122
123
124
125
126
127
128
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 122

def serializable?(value) #:nodoc:
  value.is_a?(Fixnum) || 
  value.is_a?(Array)  || 
  value.is_a?(String) || 
  value.is_a?(Hash)   || 
  value.is_a?(Bignum)
end

.stringify_keys(hash) ⇒ Object

:nodoc:



130
131
132
133
134
135
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 130

def stringify_keys(hash) #:nodoc:
  hash.inject({}) do |h, pair|
    h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
    h
  end
end

.warn_whoops(message, request, session, options = {}) ⇒ Object



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
# File 'lib/merb_whoops_notifier/whoops_notifier.rb', line 23

def warn_whoops(message, request, session, options={})
  return if request.nil?
  params = request.params

  data = {
    :error_class   => options[:error_class] || message,
    :error_message => message,
    :backtrace     => caller,
    :environment   => ENV.to_hash
  }

  data[:request] = {
    :params => params
  }

  data[:environment] = clean_whoops_environment(ENV.to_hash.merge(request.env))
  data[:environment][:app_env] = Merb.env

  data[:session] = {
     :key         => session.instance_variable_get("@session_id"),
     :data        => session.to_hash
  }

  send_to_whoops :notice => default_notice_options.merge(data)
  true
end