diff --git a/app/models/report.rb b/app/models/report.rb index dd36975790..8ca5bf8fd8 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -176,6 +176,13 @@ class Report report.error = :timeout end rescue Exception => e + # ensures that if anything unexpected prevents us from + # creating a report object we fail elegantly and log an error + if !report + Rails.logger.error("Couldn’t create report `#{type}`: <#{e.class} #{e.message}>") + return nil + end + report.error = :exception # given reports can be added by plugins we don’t want dashboard failures diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index 9cdd34cc4a..a432bb4632 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -813,6 +813,22 @@ describe Report do end end + describe "unexpected error on report initialization" do + it "returns no report" do + class ReportInitError < StandardError; end + + Report.stubs(:new).raises(ReportInitError.new("x")) + + Rails.logger.expects(:error) + .with('Couldn’t create report `signups`: ') + .once + + report = Report.find('signups') + + expect(report).to be_nil + end + end + describe 'posts' do let(:report) { Report.find('posts') }