From ddd242460b46713bc006c796ee590ae5979ebccd Mon Sep 17 00:00:00 2001 From: Raph Estrada Date: Fri, 8 Mar 2019 21:33:19 +0000 Subject: [PATCH] ErrorHandler#harshly no longer fails when backtrace nil --- lib/makara/error_handler.rb | 2 +- spec/error_handler_spec.rb | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 spec/error_handler_spec.rb diff --git a/lib/makara/error_handler.rb b/lib/makara/error_handler.rb index 1ea3c3af..d7bd7c8b 100644 --- a/lib/makara/error_handler.rb +++ b/lib/makara/error_handler.rb @@ -31,7 +31,7 @@ def gracefully(connection, e) def harshly(e) - ::Makara::Logging::Logger.log("Harshly handling: #{e}\n#{e.backtrace.join("\n\t")}") + ::Makara::Logging::Logger.log("Harshly handling: #{e}\n#{e.backtrace&.join("\n\t")}") raise e end diff --git a/spec/error_handler_spec.rb b/spec/error_handler_spec.rb new file mode 100644 index 00000000..1904c26e --- /dev/null +++ b/spec/error_handler_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' +require 'time' + +describe Makara::ErrorHandler do + let(:handler){ described_class.new } + + before{ allow(::Makara::Logging::Logger).to receive(:log) } + + describe "#harshly" do + subject{ handler.send(:harshly, error) } + + context "an error with a backtrace" do + let(:error){ StandardError.new } + + before do + error.set_backtrace(caller) + expect(error.backtrace).not_to be_nil + end + + it "re-raises the original exception and logs with a backtrace" do + expect{ subject }.to raise_error(error) + expect(::Makara::Logging::Logger).to have_received(:log).with(/Harshly handling: StandardError\s+.+/i) + end + end + + context "an error without a backtrace" do + let(:error){ StandardError.new } + + before { expect(error.backtrace).to be_nil } + + it "sends a metric, re-raises the original exception and logs without a backtrace " do + expect{ subject }.to raise_error(error) + expect(::Makara::Logging::Logger).to have_received(:log).with("Harshly handling: StandardError\n") + end + + end + end + +end