lib/rdstation/error_handler.rb in rdstation-ruby-client-1.0.0 vs lib/rdstation/error_handler.rb in rdstation-ruby-client-1.0.1

- old
+ new

@@ -1,5 +1,6 @@ +require_relative 'error/formatter' require_relative 'error_handler/conflicting_field' require_relative 'error_handler/default' require_relative 'error_handler/expired_access_token' require_relative 'error_handler/expired_code_grant' require_relative 'error_handler/invalid_credentials' @@ -21,18 +22,39 @@ def initialize(response) @response = response end def raise_errors - errors.each(&:raise_error) - # Raise only the exception message when the error is not recognized - unrecognized_error = @response['errors'] - raise unrecognized_error['error_message'] + error_types.each(&:raise_error) end private - def errors - ERROR_TYPES.map { |error| error.new(@response) } + attr_reader :response + + def array_of_errors + error_formatter.to_array.map do |error| + error.merge(additional_error_attributes) + end + end + + def error_types + ERROR_TYPES.map { |error_type| error_type.new(array_of_errors) } + end + + def response_errors + JSON.parse(response.body)['errors'] + end + + def error_formatter + @error_formatter = RDStation::Error::Formatter.new(response_errors) + end + + def additional_error_attributes + { + 'headers' => response.headers, + 'body' => JSON.parse(response.body), + 'http_status' => response.code + } end end end