1

Is there a way to integrate Airbrake with a pure Ruby project (not rails or sinatra) so that unanticipated errors get reported? I have it set up and I am able to catch errors by calling Airbrake.notify_or_ignore and passing in the exception, but I can't get it to report errors without explicitly calling this.

The following is the code that works for explicitly calling Airbrake.notify but doesn't work for sending errors to Airbrake without explicitly calling notify:

require 'airbrake'

Airbrake.configure do |config|
  config.api_key = ENV['AIRBRAKE_API_KEY']
  config.development_environments = []
  config.ignore_only = []
end

I tried adding Rack as a middleware with the following code:

require 'rack'
require 'airbrake'


Airbrake.configure do |config|
  config.api_key = ENV['AIRBRAKE_API_KEY']
  config.development_environments = []
  config.ignore_only = []
end


app = Rack::Builder.app do
    run lambda { |env| raise "Rack down" }
end

use Airbrake::Rack
run app

But I get an "undefined method `use' for main:Object (NoMethodError)"

Any thoughts?

Haskel Ash
  • 11
  • 1
  • 1
    It took me all of 30 seconds to find examples of using Airbrake in plain ruby: https://help.airbrake.io/kb/api-2/v2-api-projects-and-groups – Mark Thomas Apr 22 '15 at 00:21
  • The snippet above is for Rack application. Airbrake has a wiki page about how to use it with plain ruby: https://github.com/airbrake/airbrake/wiki/Using-Airbrake-with-plain-Ruby – Bastien Jul 14 '15 at 13:02

1 Answers1

-1

Copied from Mark's comment's link to airbrake for future googlers:

# code at http://gist.github.com/3350
# tests at http://gist.github.com/3354

class Airbrake < ActiveResource::Base
  self.site = "http://your_account.airbrake.io"

  class << self
    @@auth_token = 'your_auth_token'

    def find(*arguments)
      arguments = append_auth_token_to_params(*arguments)
      super(*arguments)
    end

    def append_auth_token_to_params(*arguments)
      opts = arguments.last.is_a?(Hash) ? arguments.pop : {}
      opts = opts.has_key?(:params) ? opts : opts.merge(:params => {}) 
      opts[:params] = opts[:params].merge(:auth_token => @@auth_token)
      arguments << opts
      arguments
    end
  end
end

class Error < Airbrake  
end

# Errors are paginated. You get 30 at a time.

@errors = Error.find :all
@errors = Error.find :all, :params => { :page => 2 }
Chase
  • 2,748
  • 2
  • 23
  • 32