【问题标题】:Automatic Airbrake errors with plain Ruby (no Rails or Sinatra)普通 Ruby 的自动 Airbrake 错误(没有 Rails 或 Sinatra)
【发布时间】:2015-06-29 08:46:06
【问题描述】:

有没有办法将 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

但我得到一个“未定义的方法‘使用’用于 main:Object (NoMethodError)”

有什么想法吗?

【问题讨论】:

标签: ruby rack airbrake


【解决方案1】:

从 Mark 的评论链接中复制到 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 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多