普通Ruby自动Airbrake错误(无Rails或Sinatra)

时间:2015-04-21 23:23:12

标签: ruby rack airbrake

有没有办法将Airbrake与纯Ruby项目(不是rails或sinatra)集成,以便报告意外错误?我已经设置好了,我可以通过调用Airbrake.notify_or_ignore并传入异常来捕获错误,但是如果没有明确地调用它,我就无法报告错误。

以下是用于显式调用Airbrake.notify的代码,但在没有明确调用notify的情况下无法向Airbrake发送错误:

require 'airbrake'

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

我尝试使用以下代码添加Rack作为中间件:

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

但是我得到一个“未定义的方法`use for main:Object(NoMethodError)”

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

从Mark的评论与未来googlers的airbrake链接中复制:

# 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 }