Skip to content

Commit

Permalink
feat: add backtrace to error output when verbose is true
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 30, 2021
1 parent 4472d48 commit abf1ef0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/pact_broker/client/environments/environment_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { "" }
Expand Down

0 comments on commit abf1ef0

Please sign in to comment.