Skip to content

Commit

Permalink
send uniqueness hash as part of the request
Browse files Browse the repository at this point in the history
  • Loading branch information
darragh committed Nov 3, 2009
1 parent 18fa900 commit 2fc7e37
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/exceptional/catcher.rb
Expand Up @@ -4,7 +4,7 @@ class << self
def handle(exception, controller=nil, request=nil)
if Config.enabled?
data = ExceptionData.new(exception, controller, request)
Remote.error(data.to_json)
Remote.error(data)
end
end
end
Expand Down
17 changes: 12 additions & 5 deletions lib/exceptional/exception_data.rb
@@ -1,3 +1,5 @@
require 'digest/md5'

module Exceptional
class ExceptionData
def initialize(exception, controller=nil, request=nil)
Expand Down Expand Up @@ -49,6 +51,15 @@ def to_hash
hash
end

def to_json
to_hash.to_json
end

def uniqueness_hash
return nil if @exception.backtrace.blank?
Digest::MD5.hexdigest(@exception.backtrace)
end

def filter_paramaters(hash)
if @controller.respond_to?(:filter_parameters)
@controller.send(:filter_parameters, hash)
Expand All @@ -73,10 +84,6 @@ def extract_http_headers(env)
headers
end

def to_json
to_hash.to_json
end

def get_hostname
require 'socket' unless defined?(Socket)
Socket.gethostname
Expand Down Expand Up @@ -114,7 +121,7 @@ def self.sanitize_hash(hash)
def self.sanitize_session(request)
session = request.session
session_hash = {}
session_hash['session_id'] = request.session_options.try(:[], :id)
session_hash['session_id'] = request.session_options ? request.session_options[:id] : nil
session_hash['session_id'] ||= session.respond_to?(:session_id) ? session.session_id : session.instance_variable_get("@session_id")
session_hash['data'] = session.respond_to?(:to_hash) ? session.to_hash : session.instance_variable_get("@data") || {}
session_hash['session_id'] ||= session_hash['data'][:session_id]
Expand Down
2 changes: 1 addition & 1 deletion lib/exceptional/integration/tester.rb
Expand Up @@ -5,7 +5,7 @@ class ExceptionalTestException <StandardError;

def self.test
data = Exceptional::ExceptionData.new(ExceptionalTestException.new)
Exceptional::Remote.error(data.to_json)
Exceptional::Remote.error(data.to_hash)
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/exceptional/remote.rb
@@ -1,18 +1,21 @@
require 'zlib'
require 'cgi'
require 'net/http'
require 'digest/md5'

module Exceptional
class Remote
class << self
def announce(json_data)
def announce(startup_data)
# post to /announcements
end

def error(json_data)
def error(exception_data)
Exceptional.logger.info "Notifying Exceptional about an error"
url = "/api/errors?api_key=#{::Exceptional::Config.api_key}&protocol_version=#{::Exceptional::PROTOCOL_VERSION}"
compressed = Zlib::Deflate.deflate(json_data, Zlib::BEST_SPEED)
uniqueness_hash = exception_data.uniqueness_hash
hash_param = uniqueness_hash.nil? ? nil: "&hash=#{uniqueness_hash}"
url = "/api/errors?api_key=#{::Exceptional::Config.api_key}&protocol_version=#{::Exceptional::PROTOCOL_VERSION}#{hash_param}"
compressed = Zlib::Deflate.deflate(exception_data.to_json, Zlib::BEST_SPEED)
call_remote(url, compressed)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/exceptional/catcher_spec.rb
Expand Up @@ -5,8 +5,8 @@
exception = mock('exception')
controller = mock('controller')
request = mock('request')
Exceptional::ExceptionData.should_receive(:new).with(exception,controller,request).and_return(mock('exception_data', :to_json =>'"json"'))
Exceptional::Remote.should_receive(:error).with('"json"')
Exceptional::ExceptionData.should_receive(:new).with(exception,controller,request).and_return(data = mock('exception_data'))
Exceptional::Remote.should_receive(:error).with(data)
Exceptional::Catcher.handle(exception,controller,request)
end
end
8 changes: 8 additions & 0 deletions spec/exceptional/exception_data_spec.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'digest/md5'

class Exceptional::FunkyError < StandardError
def backtrace
Expand Down Expand Up @@ -111,4 +112,11 @@ def initialize
@hash = Exceptional::ExceptionData.new(Exceptional::FunkyError.new('some message'), @controller, @request).to_hash
@hash['request']['headers'].should == {'Cookie' => '_something_else=faafsafafasfa; _myapp-lick-nation_session=[FILTERED]'}
end

it "creates a uniqueness_hash from backtrace" do
myException = Exception.new
myException.stub!(:backtrace).and_return('123')
data = Exceptional::ExceptionData.new(myException)
data.uniqueness_hash.should == Digest::MD5.hexdigest('123')
end
end
14 changes: 11 additions & 3 deletions spec/exceptional/remote_spec.rb
@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'zlib'
require 'digest/md5'

describe Exceptional::Remote do
before :each do
Expand All @@ -9,8 +10,15 @@

it "calls remote with api_key, protocol_version and json" do
expected_url = "/api/errors?api_key=abc123&protocol_version=#{Exceptional::PROTOCOL_VERSION}"
expected_data = '"json"'
Exceptional::Remote.should_receive(:call_remote).with(expected_url, Zlib::Deflate.deflate(expected_data,Zlib::BEST_SPEED))
Exceptional::Remote.error('"json"')
expected_data = mock('data',:uniqueness_hash => nil, :to_json => '123')
Exceptional::Remote.should_receive(:call_remote).with(expected_url, Zlib::Deflate.deflate(expected_data.to_json,Zlib::BEST_SPEED))
Exceptional::Remote.error(expected_data)
end

it "adds hash of backtrace as paramater if it is present" do
expected_url = "/api/errors?api_key=abc123&protocol_version=#{Exceptional::PROTOCOL_VERSION}&hash=blah"
expected_data = mock('data',:uniqueness_hash => 'blah', :to_json => '123')
Exceptional::Remote.should_receive(:call_remote).with(expected_url, Zlib::Deflate.deflate(expected_data.to_json,Zlib::BEST_SPEED))
Exceptional::Remote.error(expected_data)
end
end

0 comments on commit 2fc7e37

Please sign in to comment.