From 21a728f05c4b9daeb62ccb53e940a10efc8bc044 Mon Sep 17 00:00:00 2001 From: Ethan Vizitei Date: Wed, 3 Jun 2015 16:49:22 -0600 Subject: [PATCH] setup a generalized way to leave out errors from sentry reporting refs CNVS-20836 Once this is merged, plugins or extensions can register their own exceptions if they don't want them broadcast to sentry for whatever reason (there are some job failures that are a good candidate for this) Change-Id: Ic3bceb8185a92479166ec55c8c6c66dacf259f5a Reviewed-on: https://gerrit.instructure.com/55678 Tested-by: Jenkins Reviewed-by: Cody Cutrer QA-Review: August Thornton Product-Review: Ethan Vizitei --- lib/sentry_proxy.rb | 24 +++++++++++++++- spec/lib/sentry_proxy_spec.rb | 53 +++++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/lib/sentry_proxy.rb b/lib/sentry_proxy.rb index 5eacc81bd4b..04758e7e3ff 100644 --- a/lib/sentry_proxy.rb +++ b/lib/sentry_proxy.rb @@ -5,7 +5,29 @@ class SentryProxy if exception.is_a?(String) || exception.is_a?(Symbol) Raven.capture_message(exception.to_s, data) else - Raven.capture_exception(exception, data) + Raven.capture_exception(exception, data) if reportable?(exception) end end + + # There are some errors we don't care to report to sentry because + # they don't indicate a problem, but not all of them are necessarily + # in the canvas codebase (and so we might not know about them at the time we + # configure the sentry client in an initializer). This allows plugins and extensions + # to register their own errors that they don't want to get reported to sentry + def self.register_ignorable_error(error_class) + @ignorable_errors = (self.ignorable_errors << error_class).uniq + end + + def self.ignorable_errors + @ignorable_errors ||= [] + end + + def self.clear_ignorable_errors + @ignorable_errors = [] + end + + def self.reportable?(exception) + !ignorable_errors.include?(exception.class) + end + end diff --git a/spec/lib/sentry_proxy_spec.rb b/spec/lib/sentry_proxy_spec.rb index a7146f422c7..cfdfe10dbdb 100644 --- a/spec/lib/sentry_proxy_spec.rb +++ b/spec/lib/sentry_proxy_spec.rb @@ -3,21 +3,50 @@ require_relative "../spec_helper" describe SentryProxy do let(:data){ {a: 'b', c: 'd'} } - it "forwards exceptions on to raven" do - e = StandardError.new - Raven.expects(:capture_exception).with(e, data) - SentryProxy.capture(e, data) + before(:each){ SentryProxy.clear_ignorable_errors } + + class MyCustomError < StandardError end - it "passes messages to the capture_message raven method" do - e = "Some Message" - Raven.expects(:capture_message).with(e, data) - SentryProxy.capture(e, data) + describe ".capture" do + it "forwards exceptions on to raven" do + e = MyCustomError.new + Raven.expects(:capture_exception).with(e, data) + SentryProxy.capture(e, data) + end + + it "passes messages to the capture_message raven method" do + e = "Some Message" + Raven.expects(:capture_message).with(e, data) + SentryProxy.capture(e, data) + end + + it "changes symbols to strings because raven chokes otherwise" do + e = :some_exception_type + Raven.expects(:capture_message).with("some_exception_type", data) + SentryProxy.capture(e, data) + end + + it "does not send the message if configured as ignorable" do + SentryProxy.register_ignorable_error(MyCustomError) + e = MyCustomError.new + Raven.expects(:capture_exception).times(0) + SentryProxy.capture(e, data) + end end - it "changes symbols to strings because raven chokes otherwise" do - e = :some_exception_type - Raven.expects(:capture_message).with("some_exception_type", data) - SentryProxy.capture(e, data) + + describe ".register_ignorable_error" do + it "keeps track of errors we don't care about reporting" do + SentryProxy.register_ignorable_error(MyCustomError) + expect(SentryProxy.ignorable_errors).to include(MyCustomError) + end + + it "prevents the same error from being registered many times" do + start_count = SentryProxy.ignorable_errors.size + 10.times { SentryProxy.register_ignorable_error(MyCustomError) } + expect(SentryProxy.ignorable_errors.size).to eq(start_count + 1) + end end + end