Skip to content

A platform for community discussion. Free, open, simple.

FIX: ensures errors in report initialization fail nicely (#6392)

Joffrey JAFFEUXa6502ce8796d
diff --git a/app/models/report.rb b/app/models/report.rb
index dd369757904..8ca5bf8fd8e 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 9cdd34cc4a0..a432bb46321 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`: <ReportInitError x>')
+ .once
+
+ report = Report.find('signups')
+
+ expect(report).to be_nil
+ end
+ end
+
describe 'posts' do
let(:report) { Report.find('posts') }