Class: Bugsnag::Notification

Inherits:
Object
  • Object
show all
Includes:
HTTParty
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
MAX_EXCEPTIONS_TO_UNWRAP =
5

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.



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
74
75
76
77
78
79
80
# File 'lib/bugsnag/notification.rb', line 48

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

  # 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
      unless ex.is_a? Exception
        Bugsnag.warn("Converting non-Exception to RuntimeError: #{ex.inspect}")
        ex = RuntimeError.new(ex.to_s)
      end
    end

    @exceptions << ex

    if 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

#contextObject

Returns the value of attribute context.



21
22
23
# File 'lib/bugsnag/notification.rb', line 21

def context
  @context
end

#user_idObject

Returns the value of attribute user_id.



22
23
24
# File 'lib/bugsnag/notification.rb', line 22

def user_id
  @user_id
end

Class Method Details

.deliver_exception_payload(endpoint, payload) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bugsnag/notification.rb', line 25

def deliver_exception_payload(endpoint, payload)
  begin
    payload_string = Bugsnag::Helpers.dump_json(payload)

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

    response = post(endpoint, {:body => payload_string})
    Bugsnag.debug("Notification to #{endpoint} finished, response was #{response.code}, payload was #{payload_string}")
  rescue StandardError => e
    # KLUDGE: Since we don't re-raise http exceptions, this breaks rspec
    raise if e.class.to_s == "RSpec::Expectations::ExpectationNotMetError"

    Bugsnag.warn("Notification to #{endpoint} failed, #{e.inspect}")
    Bugsnag.warn(e.backtrace)
  end
end

Instance Method Details

#add_custom_data(name, value) ⇒ Object

Add a single value as custom data, to this notification



83
84
85
86
# File 'lib/bugsnag/notification.rb', line 83

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bugsnag/notification.rb', line 89

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

#deliverObject

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



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

def deliver
  return unless @configuration.should_notify?

  # Check we have at least an api_key
  if @configuration.api_key.nil?
    Bugsnag.warn "No API key configured, couldn't notify"
    return
  elsif (@configuration.api_key =~ API_KEY_REGEX).nil?
    Bugsnag.warn "Your API key (#{@configuration.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

  @meta_data = {}

  # Run the middleware here, at the end of the middleware stack, execute the actual delivery
  @configuration.middleware.run(self) do
    # Now override the required fields
    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].each do |symbol|
      if @overrides[symbol]
        self.send("#{symbol}=", @overrides[symbol])
        @overrides.delete symbol
      end
    end

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

    # Build the payload's exception event
    payload_event = {
      :releaseStage => @configuration.release_stage,
      :appVersion => @configuration.app_version,
      :context => self.context,
      :userId => self.user_id,
      :exceptions => exception_list,
      :metaData => Bugsnag::Helpers.cleanup_obj((@exceptions, @overrides), @configuration.params_filters)
    }.reject {|k,v| v.nil? }

    # Build the payload hash
    payload = {
      :apiKey => @configuration.api_key,
      :notifier => {
        :name => NOTIFIER_NAME,
        :version => NOTIFIER_VERSION,
        :url => NOTIFIER_URL
      },
      :events => [payload_event]
    }

    self.class.deliver_exception_payload(endpoint, payload)
  end
end

#exceptionsObject



187
188
189
# File 'lib/bugsnag/notification.rb', line 187

def exceptions
  @exceptions
end

#ignore?Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
# File 'lib/bugsnag/notification.rb', line 176

def ignore?
  ex = @exceptions.last
  @configuration.ignore_classes.any? do |to_ignore|
    to_ignore.is_a?(Proc) ? to_ignore.call(ex) : to_ignore == error_class(ex)
  end
end

#remove_tab(name) ⇒ Object

Remove a tab from this notification



102
103
104
105
106
# File 'lib/bugsnag/notification.rb', line 102

def remove_tab(name)
  return if name.nil?

  @meta_data.delete(name.to_sym)
end

#request_dataObject



183
184
185
# File 'lib/bugsnag/notification.rb', line 183

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