Class: Airbrake::Notice

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake/notice.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Notice

Returns a new instance of Notice.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/airbrake/notice.rb', line 99

def initialize(args)
  @args             = args
  @exception        = args[:exception]
  @api_key          = args[:api_key]
  @project_root     = args[:project_root]
  @url              = args[:url] || rack_env(:url)

  @notifier_name    = args[:notifier_name]
  @notifier_version = args[:notifier_version]
  @notifier_url     = args[:notifier_url]

  @ignore                   = args[:ignore]                   || []
  @ignore_by_filters        = args[:ignore_by_filters]        || []
  @backtrace_filters        = args[:backtrace_filters]        || []
  @params_filters           = args[:params_filters]           || []
  @params_whitelist_filters = args[:params_whitelist_filters] || []

  @parameters          = args[:parameters] ||
                               action_dispatch_params ||
                               rack_env(:params) ||
                               {}
  @component           = args[:component] || args[:controller] || parameters['controller']
  @action              = args[:action] || parameters['action']

  @environment_name = args[:environment_name]
  @cgi_data         = (args[:cgi_data].respond_to?(:to_hash) && args[:cgi_data].to_hash.dup) || args[:rack_env] || {}
  @backtrace        = Backtrace.parse(exception_attribute(:backtrace, caller), :filters => @backtrace_filters)
  @error_class      = exception_attribute(:error_class) {|exception| exception.class.name }
  @error_message    = exception_attribute(:error_message, 'Notification') do |exception|
    "#{exception.class.name}: #{args[:error_message] || exception.message}"
  end

  @hostname        = local_hostname
  @user            = args[:user] || {}


  also_use_rack_params_filters
  find_session_data

  @cleaner = args[:cleaner] ||
    Airbrake::Utils::ParamsCleaner.new(:blacklist_filters => params_filters,
                                       :whitelist_filters => params_whitelist_filters,
                                       :to_clean => data_to_clean)

  clean_data!
end

Instance Attribute Details

#actionObject (readonly)

The action (if any) that was called in this request



62
63
64
# File 'lib/airbrake/notice.rb', line 62

def action
  @action
end

#api_keyObject (readonly)

The API key for the project to which this notice should be sent



27
28
29
# File 'lib/airbrake/notice.rb', line 27

def api_key
  @api_key
end

#backtraceObject (readonly)

The backtrace from the given exception or hash.



30
31
32
# File 'lib/airbrake/notice.rb', line 30

def backtrace
  @backtrace
end

#backtrace_filtersObject (readonly)

See Configuration#backtrace_filters



45
46
47
# File 'lib/airbrake/notice.rb', line 45

def backtrace_filters
  @backtrace_filters
end

#cgi_dataObject (readonly)

CGI variables such as HTTP_METHOD



39
40
41
# File 'lib/airbrake/notice.rb', line 39

def cgi_data
  @cgi_data
end

#cleanerObject

Instance that’s used for cleaning out data that should be filtered out, should respond to #clean



95
96
97
# File 'lib/airbrake/notice.rb', line 95

def cleaner
  @cleaner
end

#componentObject (readonly) Also known as: controller

The component (if any) which was used in this request (usually the controller)



58
59
60
# File 'lib/airbrake/notice.rb', line 58

def component
  @component
end

#environment_nameObject (readonly)

The name of the server environment (such as “production”)



36
37
38
# File 'lib/airbrake/notice.rb', line 36

def environment_name
  @environment_name
end

#error_classObject (readonly)

The name of the class of error (such as RuntimeError)



33
34
35
# File 'lib/airbrake/notice.rb', line 33

def error_class
  @error_class
end

#error_messageObject (readonly)

The message from the exception, or a general description of the error



42
43
44
# File 'lib/airbrake/notice.rb', line 42

def error_message
  @error_message
end

#exceptionObject (readonly)

The exception that caused this notice, if any



24
25
26
# File 'lib/airbrake/notice.rb', line 24

def exception
  @exception
end

#hostnameObject (readonly)

The host name where this error occurred (if any)



89
90
91
# File 'lib/airbrake/notice.rb', line 89

def hostname
  @hostname
end

#ignoreObject (readonly)

See Configuration#ignore



74
75
76
# File 'lib/airbrake/notice.rb', line 74

def ignore
  @ignore
end

#ignore_by_filtersObject (readonly)

See Configuration#ignore_by_filters



77
78
79
# File 'lib/airbrake/notice.rb', line 77

def ignore_by_filters
  @ignore_by_filters
end

#notifier_nameObject (readonly)

The name of the notifier library sending this notice, such as “Airbrake Notifier”



80
81
82
# File 'lib/airbrake/notice.rb', line 80

def notifier_name
  @notifier_name
end

#notifier_urlObject (readonly)

A URL for more information about the notifier library sending this notice



86
87
88
# File 'lib/airbrake/notice.rb', line 86

def notifier_url
  @notifier_url
end

#notifier_versionObject (readonly)

The version number of the notifier library sending this notice, such as “2.1.3”



83
84
85
# File 'lib/airbrake/notice.rb', line 83

def notifier_version
  @notifier_version
end

#parametersObject (readonly) Also known as: params

A hash of parameters from the query string or post body.



54
55
56
# File 'lib/airbrake/notice.rb', line 54

def parameters
  @parameters
end

#params_filtersObject (readonly)

See Configuration#params_filters



48
49
50
# File 'lib/airbrake/notice.rb', line 48

def params_filters
  @params_filters
end

#params_whitelist_filtersObject (readonly)

See Configuration#params_whitelist_filters



51
52
53
# File 'lib/airbrake/notice.rb', line 51

def params_whitelist_filters
  @params_whitelist_filters
end

#project_rootObject (readonly)

The path to the project that caused the error (usually Rails.root)



68
69
70
# File 'lib/airbrake/notice.rb', line 68

def project_root
  @project_root
end

#session_dataObject (readonly)

A hash of session data from the request



65
66
67
# File 'lib/airbrake/notice.rb', line 65

def session_data
  @session_data
end

#urlObject (readonly)

The URL at which the error occurred (if any)



71
72
73
# File 'lib/airbrake/notice.rb', line 71

def url
  @url
end

#userObject (readonly)

Details about the user who experienced the error



92
93
94
# File 'lib/airbrake/notice.rb', line 92

def user
  @user
end

Class Method Details

.attr_reader_with_tracking(*names) ⇒ Object Also known as: attr_reader



9
10
11
12
# File 'lib/airbrake/notice.rb', line 9

def attr_reader_with_tracking(*names)
  attr_readers.concat(names)
  attr_reader_without_tracking(*names)
end

.attr_readersObject



18
19
20
# File 'lib/airbrake/notice.rb', line 18

def attr_readers
  @attr_readers ||= []
end

Instance Method Details

#[](method) ⇒ Object

Allows properties to be accessed using a hash-like syntax

Examples:

notice[:error_message]

Parameters:

  • method (String)

    The given key for an attribute

Returns:

  • The attribute value, or self if given :request



264
265
266
267
268
269
270
271
# File 'lib/airbrake/notice.rb', line 264

def [](method)
  case method
  when :request
    self
  else
    send(method)
  end
end

#ignore?Boolean

Determines if this notice should be ignored

Returns:

  • (Boolean)


253
254
255
256
# File 'lib/airbrake/notice.rb', line 253

def ignore?
  ignored_class_names.include?(error_class) ||
    ignore_by_filters.any? {|filter| filter.call(self) }
end

#to_jsonObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/airbrake/notice.rb', line 211

def to_json
  MultiJson.dump({
    'notifier' => {
      'name'    => 'airbrake',
      'version' => Airbrake::VERSION,
      'url'     => 'https://github.com/airbrake/airbrake'
      },
    'errors' => [{
        'type'       => error_class,
        'message'    => error_message,
        'backtrace'  => backtrace.lines.map do |line|
            {
              'file'     => line.file,
              'line'     => line.number.to_i,
              'function' => line.method_name
            }
        end
      }],
     'context' => {}.tap do |hash|
        if request_present?
          hash['url']           = url
          hash['component']     = controller
          hash['action']        = action
          hash['rootDirectory'] = File.dirname(project_root)
          hash['environment']   = environment_name
        end
       end.tap do |hash|
        next if user.empty?

        hash['userId']    = user[:id]
        hash['userName']  = user[:name]
        hash['userEmail'] = user[:email]
      end

  }.tap do |hash|
      hash['environment'] = cgi_data     unless cgi_data.empty?
      hash['params']      = parameters   unless parameters.empty?
      hash['session']     = session_data unless session_data.empty?
  end)
end

#to_xmlObject

Converts the given notice to XML



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/airbrake/notice.rb', line 147

def to_xml
  builder = Builder::XmlMarkup.new
  builder.instruct!
  xml = builder.notice(:version => Airbrake::API_VERSION) do |notice|
    notice.tag!("api-key", api_key)
    notice.notifier do |notifier|
      notifier.name(notifier_name)
      notifier.version(notifier_version)
      notifier.url(notifier_url)
    end
    notice.error do |error|
      error.tag!('class', error_class)
      error.message(error_message)
      error.backtrace do |backtrace|
        self.backtrace.lines.each do |line|
          backtrace.line(
            :number      => line.number,
            :file        => line.file,
            :method      => line.method_name
          )
        end
      end
    end
    if request_present?
      notice.request do |request|
        request.url(url)
        request.component(controller)
        request.action(action)
        unless parameters.empty?
          request.params do |params|
            xml_vars_for(params, parameters)
          end
        end
        unless session_data.empty?
          request.session do |session|
            xml_vars_for(session, session_data)
          end
        end
        unless cgi_data.empty?
          request.tag!("cgi-data") do |cgi_datum|
            xml_vars_for(cgi_datum, cgi_data)
          end
        end
      end
    end
    notice.tag!("server-environment") do |env|
      env.tag!("project-root", project_root)
      env.tag!("environment-name", environment_name)
      env.tag!("hostname", hostname)
    end
    unless user.empty?
      notice.tag!("current-user") do |u|
        user.each do |attr, value|
          u.tag!(attr.to_s, value)
        end
      end
    end
    if framework =~ /\S/
      notice.tag!("framework", framework)
    end
  end
  xml.to_s
end