lib/toadhopper.rb in toadhopper-1.0.3 vs lib/toadhopper.rb in toadhopper-1.0.4
- old
+ new
@@ -2,11 +2,11 @@
require 'erb'
require 'ostruct'
# Posts errors to the Hoptoad API
class Toadhopper
- VERSION = "1.0.3"
+ VERSION = "1.0.4"
FILTER_REPLACEMENT = "[FILTERED]"
# Hoptoad API response
class Response < Struct.new(:status, :body, :errors); end
@@ -50,16 +50,18 @@
def post!(error, options={}, http_headers={})
options[:notifier_name] ||= 'Toadhopper'
post_document(document_for(error, options), {'X-Hoptoad-Client-Name' => options[:notifier_name]})
end
+ private
+
def document_defaults(error)
{
:error => error,
:api_key => api_key,
:environment => ENV.to_hash,
- :backtrace => error.backtrace.map {|l| backtrace_line(l)},
+ :backtrace => backtrace_for(error),
:url => 'http://localhost/',
:component => 'http://localhost/',
:action => nil,
:request => nil,
:params => nil,
@@ -75,12 +77,10 @@
data = document_defaults(error).merge(options)
[:params, :session, :environment].each{|n| data[n] = clean(data[n]) if data[n] }
data
end
- private
-
def filters
[@filters].flatten.compact
end
def post_document(document, headers={})
@@ -105,11 +105,25 @@
data = document_data(exception, options)
scope = OpenStruct.new(data).extend(ERB::Util)
scope.instance_eval ERB.new(notice_template, nil, '-').src
end
+ BacktraceLine = Struct.new(:file, :number, :method)
+
+ def backtrace_for(error)
+ lines = Array(error.backtrace).map {|l| backtrace_line(l)}
+ if lines.empty?
+ lines << BacktraceLine.new("no-backtrace", "1", nil)
+ end
+ lines
+ end
+
def backtrace_line(line)
- Struct.new(:file, :number, :method).new(*line.match(%r{^(.+):(\d+)(?::in `([^']+)')?$}).captures)
+ if match = line.match(%r{^(.+):(\d+)(?::in `([^']+)')?$})
+ BacktraceLine.new(*match.captures)
+ else
+ BacktraceLine.new(line, "1", nil)
+ end
end
def notice_template
File.read(::File.join(::File.dirname(__FILE__), 'notice.erb'))
end