diff --git a/Gemfile b/Gemfile index 860d1cc..43864c1 100644 --- a/Gemfile +++ b/Gemfile @@ -13,4 +13,5 @@ end group :test do gem 'rspec', '>= 2.0.0.beta.19' gem 'database_cleaner', '0.5.2' + gem 'factory_girl_rails' end diff --git a/Gemfile.lock b/Gemfile.lock index 7c5f614..49fd62a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,10 @@ GEM diff-lcs (1.1.2) erubis (2.6.6) abstract (>= 1.0.0) + factory_girl (1.3.2) + factory_girl_rails (1.0) + factory_girl (~> 1.3) + rails (>= 3.0.0.beta4) haml (3.0.15) i18n (0.4.1) libxml-ruby (1.1.4) @@ -101,6 +105,7 @@ PLATFORMS DEPENDENCIES bson_ext database_cleaner (= 0.5.2) + factory_girl_rails haml libxml-ruby mongoid (= 2.0.0.beta.15) diff --git a/app/models/error.rb b/app/models/error.rb index 87ea525..a57c3a5 100644 --- a/app/models/error.rb +++ b/app/models/error.rb @@ -2,8 +2,16 @@ class Error include Mongoid::Document include Mongoid::Timestamps + field :klass + field :message + field :component + field :action + field :environment + embeds_many :notices + validates_presence_of :klass, :environment + def self.for(attrs) self.where(attrs).first || create(attrs) end diff --git a/app/models/notice.rb b/app/models/notice.rb index 8b4e5b0..0539175 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -11,11 +11,13 @@ class Notice embedded_in :error, :inverse_of => :notices + validates_presence_of :backtrace, :server_environment, :notifier + def self.from_xml(hoptoad_xml) hoptoad_notice = Hoptoad::V2.parse_xml(hoptoad_xml) error = Error.for({ - :class_name => hoptoad_notice['error']['class'], + :klass => hoptoad_notice['error']['class'], :message => hoptoad_notice['error']['message'], :component => hoptoad_notice['request']['component'], :action => hoptoad_notice['request']['action'], diff --git a/lib/hoptoad.rb b/lib/hoptoad.rb index 70bb2a0..f7a8082 100644 --- a/lib/hoptoad.rb +++ b/lib/hoptoad.rb @@ -1,8 +1,14 @@ module Hoptoad module V2 + class ApiVersionError < StandardError + def initialize + super "Wrong API Version: Expecting v2.0" + end + end def self.parse_xml(xml) parsed = ActiveSupport::XmlMini.backend.parse(xml)['notice'] + raise ApiVersionError unless parsed && parsed['version'] == '2.0' rekey(parsed) end diff --git a/spec/factories/error_factories.rb b/spec/factories/error_factories.rb new file mode 100644 index 0000000..3b0c55a --- /dev/null +++ b/spec/factories/error_factories.rb @@ -0,0 +1,24 @@ +Factory.define :error do |e| + e.klass 'FooError' + e.message 'FooError: Too Much Bar' + e.component 'foo' + e.action 'bar' + e.environment 'production' +end + +Factory.define :notice do |n| + n.error {|e| e.association :error} + n.backtrace { random_backtrace } + n.server_environment 'server-environment' => 'production' + n.notifier 'name' => 'Notifier', 'version' => '1', 'url' => 'http://toad.com' +end + +def random_backtrace + backtrace = [] + 99.times {|t| backtrace << { + 'number' => rand(999), + 'file' => "/path/to/file/#{ActiveSupport::SecureRandom.hex(4)}.rb", + 'method' => ActiveSupport.methods.shuffle.first + }} + backtrace +end \ No newline at end of file diff --git a/spec/models/error_spec.rb b/spec/models/error_spec.rb index db64cda..104a38d 100644 --- a/spec/models/error_spec.rb +++ b/spec/models/error_spec.rb @@ -2,10 +2,24 @@ require 'spec_helper' describe Error do + context 'validations' do + it 'requires a klass' do + error = Factory.build(:error, :klass => nil) + error.should_not be_valid + error.errors[:klass].should include("can't be blank") + end + + it 'requires an environment' do + error = Factory.build(:error, :environment => nil) + error.should_not be_valid + error.errors[:environment].should include("can't be blank") + end + end + context '#for' do before do @conditions = { - :class_name => 'Whoops', + :klass => 'Whoops', :message => 'Whoops: Oopsy Daisy', :component => 'Foo', :action => 'bar', diff --git a/spec/models/notice_spec.rb b/spec/models/notice_spec.rb index 3eab4ab..a1732d6 100644 --- a/spec/models/notice_spec.rb +++ b/spec/models/notice_spec.rb @@ -2,6 +2,26 @@ require 'spec_helper' describe Notice do + context 'validations' do + it 'requires a backtrace' do + notice = Factory.build(:notice, :backtrace => nil) + notice.should_not be_valid + notice.errors[:backtrace].should include("can't be blank") + end + + it 'requires the server_environment' do + notice = Factory.build(:notice, :server_environment => nil) + notice.should_not be_valid + notice.errors[:server_environment].should include("can't be blank") + end + + it 'requires the notifier' do + notice = Factory.build(:notice, :notifier => nil) + notice.should_not be_valid + notice.errors[:notifier].should include("can't be blank") + end + end + context '#from_xml' do before do @xml = Rails.root.join('spec','fixtures','hoptoad_test_notice.xml').read @@ -9,7 +29,7 @@ describe Notice do it 'finds the correct error for the notice' do Error.should_receive(:for).with({ - :class_name => 'HoptoadTestingException', + :klass => 'HoptoadTestingException', :message => 'HoptoadTestingException: Testing hoptoad via "rake hoptoad:test". If you can see this, it works.', :component => 'application', :action => 'verify', -- libgit2 0.21.2