diff --git a/lib/api_error_handler/error_reporter.rb b/lib/api_error_handler/error_reporter.rb index d6de713..d99c14b 100644 --- a/lib/api_error_handler/error_reporter.rb +++ b/lib/api_error_handler/error_reporter.rb @@ -19,11 +19,16 @@ def report(error, error_id: nil) context = error_id ? { error_id: error_id } : {} Honeybadger.notify(error, context: context) - elsif @strategy == :raven || @strategy == :sentry + elsif @strategy == :raven raise_dependency_error(missing_constant: "Raven") unless defined?(Raven) extra = error_id ? { error_id: error_id } : {} Raven.capture_exception(error, extra: extra) + elsif @strategy == :sentry + raise_dependency_error(missing_constant: "Sentry") unless defined?(Sentry) + + extra = error_id ? { error_id: error_id } : {} + Sentry.capture_exception(error, extra: extra) else raise( InvalidOptionError, diff --git a/lib/api_error_handler/version.rb b/lib/api_error_handler/version.rb index 0ca41f3..1a4ea38 100644 --- a/lib/api_error_handler/version.rb +++ b/lib/api_error_handler/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ApiErrorHandler - VERSION = "0.2.0" + VERSION = "0.2.1" end diff --git a/spec/api_error_handler/error_reporter_spec.rb b/spec/api_error_handler/error_reporter_spec.rb index a9fc088..080e146 100644 --- a/spec/api_error_handler/error_reporter_spec.rb +++ b/spec/api_error_handler/error_reporter_spec.rb @@ -56,25 +56,47 @@ end end - context "using the :raven/:sentry strategy" do - let(:reporter) { described_class.new(:sentry) } + context "using the :raven strategy" do + let(:reporter) { described_class.new(:raven) } it "Raises an error if the Raven constant is not defined" do expect { reporter.report(error) }.to raise_error(ApiErrorHandler::MissingDependencyError) end - it "Reports to Honeybadger with an error id" do + it "Reports to Sentry with an error id" do stub_const("Raven", double) expect(Raven).to receive(:capture_exception).with(error, extra: { error_id: "456" }) reporter.report(error, error_id: "456") end - it "Reports to Honeybadger without an error id" do + it "Reports to Sentry without an error id" do stub_const("Raven", double) expect(Raven).to receive(:capture_exception).with(error, extra: {}) reporter.report(error) end end + + context "using the :sentry strategy" do + let(:reporter) { described_class.new(:sentry) } + + it "Raises an error if the Sentry constant is not defined" do + expect { reporter.report(error) }.to raise_error(ApiErrorHandler::MissingDependencyError) + end + + it "Reports to Sentry with an error id" do + stub_const("Sentry", double) + expect(Sentry).to receive(:capture_exception).with(error, extra: { error_id: "456" }) + + reporter.report(error, error_id: "456") + end + + it "Reports to Sentry without an error id" do + stub_const("Sentry", double) + expect(Sentry).to receive(:capture_exception).with(error, extra: {}) + + reporter.report(error) + end + end end