From abf1ef08c6407fcf974e4b5f7947f96fc2f78303 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 30 May 2021 18:59:14 +1000 Subject: [PATCH] feat: add backtrace to error output when verbose is true --- .../environments/environment_command.rb | 30 +++++++++++++--- .../environments/update_environment_spec.rb | 35 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/pact_broker/client/environments/environment_command.rb b/lib/pact_broker/client/environments/environment_command.rb index 4d4fe64e..49bb55ca 100644 --- a/lib/pact_broker/client/environments/environment_command.rb +++ b/lib/pact_broker/client/environments/environment_command.rb @@ -29,6 +29,8 @@ def call handle_http_error(e) rescue PactBroker::Client::Error => e handle_ruby_error(e) + rescue StandardError => e + handle_ruby_error(e, pact_broker_client_options[:verbose]) end private @@ -46,15 +48,33 @@ def handle_http_error(e) PactBroker::Client::CommandResult.new(false, message) end - def handle_ruby_error(e) - message = if json_output? - { error: { message: e.message, class: e.class.name } }.to_json + def handle_ruby_error(e, include_backtrace = false) + PactBroker::Client::CommandResult.new(false, error_message(e, include_backtrace)) + end + + def error_message(e, include_backtrace) + if json_output? + json_error_message(e, include_backtrace) else - ::Term::ANSIColor.red(e.message) + text_error_message(e, include_backtrace) end - PactBroker::Client::CommandResult.new(false, message) end + def json_error_message(e, include_backtrace) + error_hash = { message: e.message } + error_hash[:class] = e.class.name unless e.is_a?(PactBroker::Client::Error) + error_hash[:backtrace] = e.backtrace if include_backtrace + { error: error_hash }.to_json + end + + + def text_error_message(e, include_backtrace) + maybe_backtrace = (include_backtrace ? "\n" + e.backtrace.join("\n") : "") + exception_message = e.is_a?(PactBroker::Client::Error) ? e.message : "#{e.class} - #{e.message}" + ::Term::ANSIColor.red(exception_message) + maybe_backtrace + end + + def new_environment_body { "name" => params[:name], diff --git a/spec/lib/pact_broker/client/environments/update_environment_spec.rb b/spec/lib/pact_broker/client/environments/update_environment_spec.rb index 20d8e76e..e2195a12 100644 --- a/spec/lib/pact_broker/client/environments/update_environment_spec.rb +++ b/spec/lib/pact_broker/client/environments/update_environment_spec.rb @@ -88,6 +88,41 @@ module Environments its(:message) { is_expected.to include "does not support environments" } end + context "when a StandardError occurs" do + before do + allow_any_instance_of(described_class).to receive(:do_call).and_raise(StandardError.new("Foo")) + end + + its(:success) { is_expected.to be false } + its(:message) { is_expected.to include "StandardError - Foo" } + + context "when verbose is on" do + let(:pact_broker_client_options) { { verbose: true } } + + it "includes the message and class and backtrace in the error" do + expect(subject.message.split("\n").size).to be > 2 + end + end + + context "when output is json" do + let(:output) { "json" } + + it "includes the message and class in the error" do + message_hash = JSON.parse(subject.message) + expect(message_hash).to eq "error" => { "message" => "Foo", "class" => "StandardError" } + end + + context "when verbose is on" do + let(:pact_broker_client_options) { { verbose: true } } + + it "includes the message and class and backtrace in the error" do + message_hash = JSON.parse(subject.message) + expect(message_hash["error"]["backtrace"]).to be_a(Array) + end + end + end + end + context "when the environment does not exist" do let(:get_environment_response_status) { 404 } let(:get_environment_response_body) { "" }