diff --git a/lib/pact_broker/client/base_command.rb b/lib/pact_broker/client/base_command.rb new file mode 100644 index 00000000..f85b1762 --- /dev/null +++ b/lib/pact_broker/client/base_command.rb @@ -0,0 +1,87 @@ +require 'pact_broker/client/hal_client_methods' +require 'pact_broker/client/error' +require 'pact_broker/client/command_result' +require 'term/ansicolor' +require 'pact_broker/client/backports' + +module PactBroker + module Client + class BaseCommand + include PactBroker::Client::HalClientMethods + + def self.call(params, options, pact_broker_client_options) + new(params, options, pact_broker_client_options).call + end + + def initialize(params, options, pact_broker_client_options) + @params = params + @options = options + @pact_broker_base_url = pact_broker_client_options.fetch(:pact_broker_base_url) + @pact_broker_client_options = pact_broker_client_options + end + + def call + check_if_command_supported + do_call + rescue PactBroker::Client::Hal::ErrorResponseReturned => e + handle_http_error(e) + rescue PactBroker::Client::Error => e + handle_ruby_error(e) + rescue StandardError => e + handle_ruby_error(e, verbose?) + end + + private + + attr_reader :params, :options + attr_reader :pact_broker_base_url, :pact_broker_client_options + + def handle_http_error(e) + message = if json_output? + body = e.entity.response.raw_body + (body.nil? || body == "") ? "{}" : body + else + ::Term::ANSIColor.red(e.message) + end + PactBroker::Client::CommandResult.new(false, message) + end + + 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 + text_error_message(e, include_backtrace) + end + 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 check_if_command_supported + end + + def json_output? + options[:output] == "json" + end + + def verbose? + options[:verbose] + end + end + end +end diff --git a/lib/pact_broker/client/environments/environment_command.rb b/lib/pact_broker/client/environments/environment_command.rb index c3b6f564..b2bed330 100644 --- a/lib/pact_broker/client/environments/environment_command.rb +++ b/lib/pact_broker/client/environments/environment_command.rb @@ -1,81 +1,13 @@ -require 'pact_broker/client/hal_client_methods' -require 'pact_broker/client/error' -require 'pact_broker/client/command_result' -require 'term/ansicolor' -require 'pact_broker/client/backports' +require 'pact_broker/client/base_command' module PactBroker module Client module Environments - class EnvironmentCommand - include PactBroker::Client::HalClientMethods - + class EnvironmentCommand < PactBroker::Client::BaseCommand NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support environments. Please upgrade to version 2.80.0 or later." - def self.call(params, options, pact_broker_client_options) - new(params, options, pact_broker_client_options).call - end - - def initialize(params, options, pact_broker_client_options) - @params = params - @options = options - @pact_broker_base_url = pact_broker_client_options.fetch(:pact_broker_base_url) - @pact_broker_client_options = pact_broker_client_options - end - - def call - check_if_command_supported - do_call - rescue PactBroker::Client::Hal::ErrorResponseReturned => e - handle_http_error(e) - rescue PactBroker::Client::Error => e - handle_ruby_error(e) - rescue StandardError => e - handle_ruby_error(e, verbose?) - end - private - attr_reader :params, :options - attr_reader :pact_broker_base_url, :pact_broker_client_options - - def handle_http_error(e) - message = if json_output? - body = e.entity.response.raw_body - (body.nil? || body == "") ? "{}" : body - else - ::Term::ANSIColor.red(e.message) - end - PactBroker::Client::CommandResult.new(false, message) - end - - 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 - text_error_message(e, include_backtrace) - end - 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], @@ -128,14 +60,6 @@ def check_if_command_supported raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE) end end - - def json_output? - options[:output] == "json" - end - - def verbose? - options[:verbose] - end end end end