ruby-on-railsairbrake

notify_airbrake throws error on custom classes


We use airbrake throughout our codebase which works great except for custom classes that do not inherit from a Rails hierarchy (i.e. ActiveRecord).

Some code:

class Offer
  def counter(amount, qty=1)
    begin
      offers_response = viyet_mage_rest_client.counter_offer(@id.to_i, amount, qty.to_i)
    rescue => e
      notify_airbrake(e)
      offers_response = []
    end
  end
end

In airbrake we see:

NoMethodError: undefined method `notify_airbrake' for Offer:Class

As the error. I want the actual error and not an error for reporting an error!

Our airbrake.rb file:

if Rails.env.production? || Rails.env.staging? 
  Airbrake.configure do |config|
    config.api_key = Settings.airbrake.api_key
    config.secure = true
  end
end

Any help would be appreciated!


Solution

  • The method you need is Airbrake.notify_or_ignore(). For your given example:

    class Offer
      def counter(amount, qty=1)
        begin
          offers_response = viyet_mage_rest_client.counter_offer(@id.to_i, amount, qty.to_i)
        rescue => e
          Airbrake.notify_or_ignore(e)
          offers_response = []
        end
      end
    end
    

    More detail can be found here: https://github.com/airbrake/airbrake/wiki/Using-Airbrake-with-plain-Ruby