Class: Bugsnag::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/notification.rb

Constant Summary collapse

NOTIFIER_NAME =
"Ruby Bugsnag Notifier"
NOTIFIER_VERSION =
Bugsnag::VERSION
NOTIFIER_URL =
"http://www.bugsnag.com"
API_KEY_REGEX =
/[0-9a-f]{32}/i
BACKTRACE_LINE_REGEX =

e.g. “org/jruby/RubyKernel.java:1264:in ‘catch’”

/^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$/
JAVA_BACKTRACE_REGEX =

e.g. “org.jruby.Ruby.runScript(Ruby.java:807)”

/^(.*)\((.*)(?::([0-9]+))?\)$/
MAX_EXCEPTIONS_TO_UNWRAP =
5
SUPPORTED_SEVERITIES =
["error", "warning", "info"]
CURRENT_PAYLOAD_VERSION =
"2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, configuration, overrides = nil, request_data = nil) ⇒ Notification

Returns a new instance of Notification.



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
78
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
104
105
106
107
108
109
110
# File 'lib/bugsnag/notification.rb', line 52

def initialize(exception, configuration, overrides = nil, request_data = nil)
  @configuration = configuration
  @overrides = Bugsnag::Helpers.(overrides) || {}
  @request_data = request_data
  @meta_data = {}
  @user = {}
  @should_ignore = false

  self.severity = @overrides[:severity]
  @overrides.delete :severity

  if @overrides.key? :grouping_hash
    self.grouping_hash = @overrides[:grouping_hash]
    @overrides.delete :grouping_hash
  end

  if @overrides.key? :api_key
    self.api_key = @overrides[:api_key]
    @overrides.delete :api_key
  end

  if @overrides.key? :delivery_method
    @delivery_method = @overrides[:delivery_method]
    @overrides.delete :delivery_method
  end

  # Unwrap exceptions
  @exceptions = []

  ex = exception
  while ex != nil && !@exceptions.include?(ex) && @exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP

    unless ex.is_a? Exception
      if ex.respond_to?(:to_exception)
        ex = ex.to_exception
      elsif ex.respond_to?(:exception)
        ex = ex.exception
      end
    end

    unless ex.is_a?(Exception) || (defined?(Java::JavaLang::Throwable) && ex.is_a?(Java::JavaLang::Throwable))
      Bugsnag.warn("Converting non-Exception to RuntimeError: #{ex.inspect}")
      ex = RuntimeError.new(ex.to_s)
      ex.set_backtrace caller
    end

    @exceptions << ex

    if ex.respond_to?(:cause) && ex.cause
      ex = ex.cause
    elsif ex.respond_to?(:continued_exception) && ex.continued_exception
      ex = ex.continued_exception
    elsif ex.respond_to?(:original_exception) && ex.original_exception
      ex = ex.original_exception
    else
      ex = nil
    end
  end
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



34
35
36
# File 'lib/bugsnag/notification.rb', line 34

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



32
33
34
# File 'lib/bugsnag/notification.rb', line 32

def context
  @context
end

#meta_dataObject

Returns the value of attribute meta_data.



35
36
37
# File 'lib/bugsnag/notification.rb', line 35

def 
  @meta_data
end

#userObject

Returns the value of attribute user.



33
34
35
# File 'lib/bugsnag/notification.rb', line 33

def user
  @user
end

Class Method Details

.deliver_exception_payload(url, payload, configuration = Bugsnag.configuration, delivery_method = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bugsnag/notification.rb', line 38

def deliver_exception_payload(url, payload, configuration=Bugsnag.configuration, delivery_method=nil)

  # If the payload is going to be too long, we trim the hashes to send
  # a minimal payload instead
  payload_string = ::JSON.dump(payload)
  if payload_string.length > 128000
    payload[:events].each {|e| e[:metaData] = Bugsnag::Helpers.reduce_hash_size(e[:metaData])}
    payload_string = ::JSON.dump(payload)
  end

  Bugsnag::Delivery[delivery_method || configuration.delivery_method].deliver(url, payload_string, configuration)
end

Instance Method Details

#add_custom_data(name, value) ⇒ Object

Add a single value as custom data, to this notification



113
114
115
116
# File 'lib/bugsnag/notification.rb', line 113

def add_custom_data(name, value)
  @meta_data[:custom] ||= {}
  @meta_data[:custom][name.to_sym] = value
end

#add_tab(name, value) ⇒ Object

Add a new tab to this notification



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bugsnag/notification.rb', line 119

def add_tab(name, value)
  return if name.nil?

  if value.is_a? Hash
    @meta_data[name.to_sym] ||= {}
    @meta_data[name.to_sym].merge! value
  else
    self.add_custom_data(name, value)
    Bugsnag.warn "Adding a tab requires a hash, adding to custom tab instead (name=#{name})"
  end
end

#api_keyObject



175
176
177
# File 'lib/bugsnag/notification.rb', line 175

def api_key
  @api_key ||= @configuration.api_key
end

#api_key=(api_key) ⇒ Object



171
172
173
# File 'lib/bugsnag/notification.rb', line 171

def api_key=(api_key)
  @api_key = api_key
end

#build_exception_payloadObject

Build an exception payload



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bugsnag/notification.rb', line 234

def build_exception_payload
  # Build the payload's exception event
  payload_event = {
    :app => {
      :version => @configuration.app_version,
      :releaseStage => @configuration.release_stage,
      :type => @configuration.app_type
    },
    :context => self.context,
    :user => @user,
    :payloadVersion => payload_version,
    :exceptions => exception_list,
    :severity => self.severity,
    :groupingHash => self.grouping_hash,
  }

  payload_event[:device] = {:hostname => @configuration.hostname} if @configuration.hostname

  # cleanup character encodings
  payload_event = Bugsnag::Cleaner.clean_object_encoding(payload_event)

  # filter out sensitive values in (and cleanup encodings) metaData
  payload_event[:metaData] = Bugsnag::Cleaner.new(@configuration.params_filters).clean_object(@meta_data)
  payload_event.reject! {|k,v| v.nil? }

  # return the payload hash
  {
    :apiKey => api_key,
    :notifier => {
      :name => NOTIFIER_NAME,
      :version => NOTIFIER_VERSION,
      :url => NOTIFIER_URL
    },
    :events => [payload_event]
  }
end

#deliverObject

Deliver this notification to bugsnag.com Also runs through the middleware as required.



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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/bugsnag/notification.rb', line 180

def deliver
  return unless @configuration.should_notify?

  # Check we have at least an api_key
  if api_key.nil?
    Bugsnag.warn "No API key configured, couldn't notify"
    return
  elsif api_key !~ API_KEY_REGEX
    Bugsnag.warn "Your API key (#{api_key}) is not valid, couldn't notify"
    return
  end

  # Warn if no release_stage is set
  Bugsnag.warn "You should set your app's release_stage (see https://bugsnag.com/docs/notifiers/ruby#release_stage)." unless @configuration.release_stage

  @configuration.internal_middleware.run(self)

  exceptions.each do |exception|
    if exception.class.include?(Bugsnag::MetaData)
      if exception.bugsnag_user_id.is_a?(String)
        self.user_id = exception.bugsnag_user_id
      end
      if exception.bugsnag_context.is_a?(String)
        self.context = exception.bugsnag_context
      end
    end
  end

  [:user_id, :context, :user, :grouping_hash].each do |symbol|
    if @overrides[symbol]
      self.send("#{symbol}=", @overrides[symbol])
      @overrides.delete symbol
    end
  end

  # make meta_data available to public middleware
  @meta_data = (@exceptions, @overrides)

  # Run the middleware here (including Bugsnag::Middleware::Callbacks)
  # at the end of the middleware stack, execute the actual notification delivery
  @configuration.middleware.run(self) do
    # This supports self.ignore! for before_notify_callbacks.
    return if @should_ignore

    # Build the endpoint url
    endpoint = (@configuration.use_ssl ? "https://" : "http://") + @configuration.endpoint
    Bugsnag.log("Notifying #{endpoint} of #{@exceptions.last.class} from api_key #{api_key}")

    # Deliver the payload
    self.class.deliver_exception_payload(endpoint, build_exception_payload, @configuration, @delivery_method)
  end
end

#exceptionsObject



279
280
281
# File 'lib/bugsnag/notification.rb', line 279

def exceptions
  @exceptions
end

#grouping_hashObject



167
168
169
# File 'lib/bugsnag/notification.rb', line 167

def grouping_hash
  @grouping_hash || nil
end

#grouping_hash=(grouping_hash) ⇒ Object



163
164
165
# File 'lib/bugsnag/notification.rb', line 163

def grouping_hash=(grouping_hash)
  @grouping_hash = grouping_hash
end

#ignore!Object



283
284
285
# File 'lib/bugsnag/notification.rb', line 283

def ignore!
  @should_ignore = true
end

#ignore?Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/bugsnag/notification.rb', line 271

def ignore?
  @should_ignore || ignore_exception_class? || ignore_user_agent?
end

#payload_versionObject



159
160
161
# File 'lib/bugsnag/notification.rb', line 159

def payload_version
  CURRENT_PAYLOAD_VERSION
end

#remove_tab(name) ⇒ Object

Remove a tab from this notification



132
133
134
135
136
# File 'lib/bugsnag/notification.rb', line 132

def remove_tab(name)
  return if name.nil?

  @meta_data.delete(name.to_sym)
end

#request_dataObject



275
276
277
# File 'lib/bugsnag/notification.rb', line 275

def request_data
  @request_data || Bugsnag.configuration.request_data
end

#severityObject



155
156
157
# File 'lib/bugsnag/notification.rb', line 155

def severity
  @severity || "warning"
end

#severity=(severity) ⇒ Object



151
152
153
# File 'lib/bugsnag/notification.rb', line 151

def severity=(severity)
  @severity = severity if SUPPORTED_SEVERITIES.include?(severity)
end

#user_idObject



142
143
144
# File 'lib/bugsnag/notification.rb', line 142

def user_id
  @user[:id]
end

#user_id=(user_id) ⇒ Object



138
139
140
# File 'lib/bugsnag/notification.rb', line 138

def user_id=(user_id)
  @user[:id] = user_id
end