From 8a7183a37003b246f6aaf331b5a021662f85a9e5 Mon Sep 17 00:00:00 2001 From: Cyril Mougel Date: Mon, 6 May 2013 20:28:07 +0200 Subject: [PATCH] Extract completly the ErrorReport class --- app/models/app.rb | 38 -------------------------------------- app/models/error_report.rb | 14 ++++++++++++++ lib/tasks/errbit/demo.rake | 14 ++++++-------- spec/controllers/notices_controller_spec.rb | 15 --------------- spec/models/app_spec.rb | 22 ---------------------- spec/models/error_report_spec.rb | 17 ++++++++++++++++- 6 files changed, 36 insertions(+), 84 deletions(-) diff --git a/app/models/app.rb b/app/models/app.rb index f4846d3..a979c96 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -42,44 +42,6 @@ class App accepts_nested_attributes_for :notification_service, :allow_destroy => true, :reject_if => proc { |attrs| !NotificationService.subclasses.map(&:to_s).include?(attrs[:type].to_s) } - # Processes a new error report. - # - # Accepts either XML or a hash with the following attributes: - # - # * :error_class - the class of error - # * :message - the error message - # * :backtrace - an array of stack trace lines - # - # * :request - a hash of values describing the request - # * :server_environment - a hash of values describing the server environment - # - # * :api_key - the API key with which the error was reported - # * :notifier - information to identify the source of the error report - # - def self.report_error!(*args) - report = ErrorReport.new(*args) - report.generate_notice! - end - - - # Processes a new error report. - # - # Accepts a hash with the following attributes: - # - # * :error_class - the class of error - # * :message - the error message - # * :backtrace - an array of stack trace lines - # - # * :request - a hash of values describing the request - # * :server_environment - a hash of values describing the server environment - # - # * :notifier - information to identify the source of the error report - # - def report_error!(hash) - report = ErrorReport.new(hash.merge(:api_key => api_key)) - report.generate_notice! - end - def find_or_create_err!(attrs) Err.where( :fingerprint => attrs[:fingerprint] diff --git a/app/models/error_report.rb b/app/models/error_report.rb index 24e2b5d..56ae893 100644 --- a/app/models/error_report.rb +++ b/app/models/error_report.rb @@ -1,6 +1,20 @@ require 'digest/sha1' require 'hoptoad_notifier' +## +# Processes a new error report. +# +# Accepts a hash with the following attributes: +# +# * :error_class - the class of error +# * :message - the error message +# * :backtrace - an array of stack trace lines +# +# * :request - a hash of values describing the request +# * :server_environment - a hash of values describing the server environment +# +# * :notifier - information to identify the source of the error report +# class ErrorReport attr_reader :error_class, :message, :request, :server_environment, :api_key, :notifier, :user_attributes, :framework diff --git a/lib/tasks/errbit/demo.rake b/lib/tasks/errbit/demo.rake index 795ff01..bd1d490 100644 --- a/lib/tasks/errbit/demo.rake +++ b/lib/tasks/errbit/demo.rake @@ -42,15 +42,15 @@ namespace :errbit do errors.each do |error_template| rand(34).times do - - error_report = error_template.reverse_merge({ + ErrorReport.new(error_template.reverse_merge({ + :api_key => app.api_key, :error_class => "StandardError", :message => "Oops. Something went wrong!", :backtrace => random_backtrace, :request => { - 'component' => 'main', - 'action' => 'error' - }, + 'component' => 'main', + 'action' => 'error' + }, :server_environment => {'environment-name' => Rails.env.to_s}, :notifier => {:name => "seeds.rb"}, :app_user => { @@ -59,9 +59,7 @@ namespace :errbit do :name => "John Smith", :url => "http://www.example.com/users/jsmith" } - }) - - app.report_error!(error_report) + })).generate_notice! end end diff --git a/spec/controllers/notices_controller_spec.rb b/spec/controllers/notices_controller_spec.rb index 0fc686c..5a260f8 100644 --- a/spec/controllers/notices_controller_spec.rb +++ b/spec/controllers/notices_controller_spec.rb @@ -77,21 +77,6 @@ describe NoticesController do expect(response.status).to eq 422 end end - - # Not relevant here, Nee to be test on App.report_error! test - it "sends a notification email", :pending => true do - App.should_receive(:report_error!).with(xml).and_return(notice) - request.should_receive(:raw_post).and_return(xml) - post :create, :format => :xml - response.should be_success - response.body.should match(%r{]*>#{notice.id}}) - response.body.should match(%r{]*>(.+)#{locate_path(notice.id)}}) - email = ActionMailer::Base.deliveries.last - email.to.should include(app.watchers.first.email) - email.subject.should include(notice.message.truncate(50)) - email.subject.should include("[#{app.name}]") - email.subject.should include("[#{notice.environment_name}]") - end end describe "GET /locate/:id" do diff --git a/spec/models/app_spec.rb b/spec/models/app_spec.rb index 3b7cfaf..e67bca1 100644 --- a/spec/models/app_spec.rb +++ b/spec/models/app_spec.rb @@ -187,28 +187,6 @@ describe App do end - context '#report_error!', :pending => true do - # method delete. test need to be on spec/models/error_report - before do - @xml = Rails.root.join('spec','fixtures','hoptoad_test_notice.xml').read - @app = Fabricate(:app, :api_key => 'APIKEY') - ErrorReport.any_instance.stub(:fingerprint).and_return('fingerprintdigest') - end - - it "should handle params without 'request' section" do - xml = Rails.root.join('spec','fixtures','hoptoad_test_notice_without_request_section.xml').read - lambda { App.report_error!(xml) }.should_not raise_error - end - - it "should handle params with only a single line of backtrace" do - xml = Rails.root.join('spec','fixtures','hoptoad_test_notice_with_one_line_of_backtrace.xml').read - lambda { @notice = App.report_error!(xml) }.should_not raise_error - @notice.backtrace_lines.length.should == 1 - end - - - end - describe ".find_by_api_key!" do it 'return the app with api_key' do app = Fabricate(:app) diff --git a/spec/models/error_report_spec.rb b/spec/models/error_report_spec.rb index 89d2dd7..72fcd2c 100644 --- a/spec/models/error_report_spec.rb +++ b/spec/models/error_report_spec.rb @@ -97,6 +97,22 @@ describe ErrorReport do }.from(true).to(false) end + context "with notification service configured" do + before do + app.notify_on_errs = true + app.watchers.build(:email => 'foo@example.com') + app.save + end + it 'send email' do + notice = error_report.generate_notice! + email = ActionMailer::Base.deliveries.last + email.to.should include(app.watchers.first.email) + email.subject.should include(notice.message.truncate(50)) + email.subject.should include("[#{app.name}]") + email.subject.should include("[#{notice.environment_name}]") + end + end + context "with xml without request section" do let(:xml){ Rails.root.join('spec','fixtures','hoptoad_test_notice_without_request_section.xml').read @@ -159,6 +175,5 @@ describe ErrorReport do end end - end end -- libgit2 0.21.2