Class: Bugsnag::Report

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

Overview

rubocop:todo Metrics/ClassLength

Constant Summary collapse

NOTIFIER_NAME =
"Ruby Bugsnag Notifier"
NOTIFIER_VERSION =
Bugsnag::VERSION
NOTIFIER_URL =
"https://www.bugsnag.com"
UNHANDLED_EXCEPTION =
"unhandledException"
UNHANDLED_EXCEPTION_MIDDLEWARE =
"unhandledExceptionMiddleware"
ERROR_CLASS =
"errorClass"
HANDLED_EXCEPTION =
"handledException"
USER_SPECIFIED_SEVERITY =
"userSpecifiedSeverity"
USER_CALLBACK_SET_SEVERITY =
"userCallbackSetSeverity"
MAX_EXCEPTIONS_TO_UNWRAP =
5
CURRENT_PAYLOAD_VERSION =
"4.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, passed_configuration, auto_notify = false) ⇒ Report

Initializes a new report from an exception.



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
145
146
# File 'lib/bugsnag/report.rb', line 116

def initialize(exception, passed_configuration, auto_notify=false)
  # store the creation time for use as device.time
  @created_at = Time.now.utc.iso8601(3)

  @should_ignore = false
  @unhandled = auto_notify
  @initial_unhandled = @unhandled

  self.configuration = passed_configuration

  @original_error = exception
  self.raw_exceptions = generate_raw_exceptions(exception)
  self.exceptions = generate_exception_list
  @errors = generate_error_list

  self.api_key = configuration.api_key
  self.app_type = configuration.app_type
  self.app_version = configuration.app_version
  self.breadcrumbs = []
  self.context = configuration.context if configuration.context_set?
  self.delivery_method = configuration.delivery_method
  self.hostname = configuration.hostname
  self.runtime_versions = configuration.runtime_versions.dup
  self. = Utility::Duplicator.duplicate(configuration.)
  self.release_stage = configuration.release_stage
  self.severity = auto_notify ? "error" : "warning"
  self.severity_reason = auto_notify ? {:type => UNHANDLED_EXCEPTION} : {:type => HANDLED_EXCEPTION}
  self.user = {}

  @metadata_delegate = Utility::MetadataDelegate.new
end

Instance Attribute Details

#api_keyString?

Your Integration API Key

Returns:

  • (String, nil)

See Also:



31
32
33
# File 'lib/bugsnag/report.rb', line 31

def api_key
  @api_key
end

#app_typeString?

The type of application executing the current code

Returns:

  • (String, nil)

See Also:



36
37
38
# File 'lib/bugsnag/report.rb', line 36

def app_type
  @app_type
end

#app_versionString?

The current version of your application

Returns:

  • (String, nil)


40
41
42
# File 'lib/bugsnag/report.rb', line 40

def app_version
  @app_version
end

The list of breadcrumbs attached to this report

Returns:

  • (Array<Breadcrumb>)


44
45
46
# File 'lib/bugsnag/report.rb', line 44

def breadcrumbs
  @breadcrumbs
end

#contextString?

Additional context for this report

Returns:

  • (String, nil)


152
153
154
155
156
# File 'lib/bugsnag/report.rb', line 152

def context
  return @context if defined?(@context)

  @automatic_context
end

#delivery_methodSymbol

The delivery method that will be used for this report

Returns:

  • (Symbol)

See Also:



53
54
55
# File 'lib/bugsnag/report.rb', line 53

def delivery_method
  @delivery_method
end

#errorsArray<Error> (readonly)

A list of errors in this report

Returns:



108
109
110
# File 'lib/bugsnag/report.rb', line 108

def errors
  @errors
end

#exceptionsArray<Hash>

Deprecated.

Use #errors instead

The list of exceptions in this report

Returns:

  • (Array<Hash>)


58
59
60
# File 'lib/bugsnag/report.rb', line 58

def exceptions
  @exceptions
end

#grouping_hashString

All errors with the same grouping hash will be grouped in the Bugsnag app

Returns:

  • (String)


71
72
73
# File 'lib/bugsnag/report.rb', line 71

def grouping_hash
  @grouping_hash
end

#hostnameString

Returns:

  • (String)

See Also:



62
63
64
# File 'lib/bugsnag/report.rb', line 62

def hostname
  @hostname
end

#meta_dataHash

Deprecated.

Use #metadata instead

Arbitrary metadata attached to this report

Returns:

  • (Hash)


76
77
78
# File 'lib/bugsnag/report.rb', line 76

def 
  @meta_data
end

#metadataHash

A Hash containing arbitrary metadata

Returns:

  • (Hash)


305
306
307
# File 'lib/bugsnag/report.rb', line 305

def 
  @meta_data
end

#original_errorException (readonly)

The Exception instance this report was created for

Returns:

  • (Exception)


112
113
114
# File 'lib/bugsnag/report.rb', line 112

def original_error
  @original_error
end

#raw_exceptionsArray<Exception>

Deprecated.

Use #original_error instead

The raw Exception instances for this report

Returns:

  • (Array<Exception>)

See Also:



82
83
84
# File 'lib/bugsnag/report.rb', line 82

def raw_exceptions
  @raw_exceptions
end

#release_stageString?

The current stage of the release process, e.g. 'development', production'

Returns:

  • (String, nil)

See Also:



87
88
89
# File 'lib/bugsnag/report.rb', line 87

def release_stage
  @release_stage
end

#sessionHash

The session that active when this report was generated

Returns:

  • (Hash)

See Also:



92
93
94
# File 'lib/bugsnag/report.rb', line 92

def session
  @session
end

#severityString

The severity of this report, e.g. 'error', 'warning'

Returns:

  • (String)


96
97
98
# File 'lib/bugsnag/report.rb', line 96

def severity
  @severity
end

#unhandledBoolean

Whether this report is for a handled or unhandled error

Returns:

  • (Boolean)


26
27
28
# File 'lib/bugsnag/report.rb', line 26

def unhandled
  @unhandled
end

#userHash

The current user when this report was generated

Returns:

  • (Hash)


104
105
106
# File 'lib/bugsnag/report.rb', line 104

def user
  @user
end

Instance Method Details

#add_metadata(section, data) ⇒ void #add_metadata(section, key, value) ⇒ void

This method returns an undefined value.

Add values to metadata

Overloads:

  • #add_metadata(section, data) ⇒ void

    Merges data into the given section of metadata

    Parameters:

    • section (String, Symbol)
    • data (Hash)
  • #add_metadata(section, key, value) ⇒ void

    Sets key to value in the given section of metadata. If the value is nil the key will be deleted

    Parameters:

    • section (String, Symbol)
    • key (String, Symbol)
    • value


339
340
341
# File 'lib/bugsnag/report.rb', line 339

def (section, key_or_data, *args)
  @metadata_delegate.(@meta_data, section, key_or_data, *args)
end

#add_tab(name, value) ⇒ void

Deprecated.

Use #add_metadata instead

This method returns an undefined value.

Add a new metadata tab to this notification.

Parameters:

  • name (String, #to_s)

    The name of the tab to add

  • value (Hash, Object)

    The value to add to the tab. If the tab already exists, this will be merged with the existing values. If a Hash is not given, the value will be placed into the 'custom' tab



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/bugsnag/report.rb', line 177

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

  if value.is_a? Hash
    [name] ||= {}
    [name].merge! value
  else
    ["custom"] = {} unless ["custom"]

    ["custom"][name.to_s] = value
  end
end

#as_jsonHash

Builds and returns the exception payload for this notification.

Returns:

  • (Hash)


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
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/bugsnag/report.rb', line 207

def as_json
  # Build the payload's exception event
  payload_event = {
    app: {
      version: app_version,
      releaseStage: release_stage,
      type: app_type
    },
    breadcrumbs: breadcrumbs.map(&:to_h),
    context: context,
    device: {
      hostname: hostname,
      runtimeVersions: runtime_versions,
      time: @created_at
    },
    exceptions: exceptions,
    groupingHash: grouping_hash,
    metaData: ,
    session: session,
    severity: severity,
    severityReason: severity_reason,
    unhandled: @unhandled,
    user: user
  }

  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

#clear_metadata(section) ⇒ void #clear_metadata(section, key) ⇒ void

This method returns an undefined value.

Clear values from metadata

Overloads:

  • #clear_metadata(section) ⇒ void

    Clears the given section of metadata

    Parameters:

    • section (String, Symbol)
  • #clear_metadata(section, key) ⇒ void

    Clears the key in the given section of metadata

    Parameters:

    • section (String, Symbol)
    • key (String, Symbol)


356
357
358
# File 'lib/bugsnag/report.rb', line 356

def (section, *args)
  @metadata_delegate.(@meta_data, section, *args)
end

#headersHash{String => String}

Returns the headers required for the notification.

Returns:

  • (Hash{String => String})


250
251
252
253
254
255
256
# File 'lib/bugsnag/report.rb', line 250

def headers
  {
    "Bugsnag-Api-Key" => api_key,
    "Bugsnag-Payload-Version" => CURRENT_PAYLOAD_VERSION,
    "Bugsnag-Sent-At" => Time.now.utc.iso8601(3)
  }
end

#ignore!void

This method returns an undefined value.

Tells the client this report should not be sent.



278
279
280
# File 'lib/bugsnag/report.rb', line 278

def ignore!
  @should_ignore = true
end

#ignore?Boolean

Whether this report should be ignored and not sent.

Returns:

  • (Boolean)


262
263
264
# File 'lib/bugsnag/report.rb', line 262

def ignore?
  @should_ignore
end

#remove_tab(name) ⇒ void

Deprecated.

Use #clear_metadata instead

This method returns an undefined value.

Removes a metadata tab from this notification.

Parameters:

  • name (String)


197
198
199
200
201
# File 'lib/bugsnag/report.rb', line 197

def remove_tab(name)
  return if name.nil?

  .delete(name)
end

#requestHash?

Data from the current HTTP request. May be nil if no data has been recorded

Returns:

  • (Hash, nil)


319
320
321
# File 'lib/bugsnag/report.rb', line 319

def request
  @meta_data[:request]
end

#request_dataHash

Data set on the configuration to be attached to every error notification.

Returns:

  • (Hash)


270
271
272
# File 'lib/bugsnag/report.rb', line 270

def request_data
  configuration.request_data
end

#set_user(id = nil, email = nil, name = nil) ⇒ void

This method returns an undefined value.

Set information about the current user

Additional user fields can be added as metadata in a “user” section

Setting a field to 'nil' will remove it from the user data

Parameters:

  • id (String, nil) (defaults to: nil)
  • email (String, nil) (defaults to: nil)
  • name (String, nil) (defaults to: nil)


371
372
373
374
375
376
# File 'lib/bugsnag/report.rb', line 371

def set_user(id = nil, email = nil, name = nil)
  new_user = { id: id, email: email, name: name }
  new_user.reject! { |key, value| value.nil? }

  @user = new_user
end

#summaryHash

Generates a summary to be attached as a breadcrumb

Returns:

  • (Hash)

    a Hash containing the report's error class, error message, and severity



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/bugsnag/report.rb', line 286

def summary
  # Guard against the exceptions array being removed/changed or emptied here
  if exceptions.respond_to?(:first) && exceptions.first
    {
      :error_class => exceptions.first[:errorClass],
      :message => exceptions.first[:message],
      :severity => severity
    }
  else
    {
      :error_class => "Unknown",
      :severity => severity
    }
  end
end