From d4aecaee2041133bb68fa4df6ab580114c416dbd Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Tue, 21 Jan 2014 11:16:54 -0600 Subject: [PATCH] adding request and index to error messages --- lib/neography/connection.rb | 23 +++++++++++++---------- lib/neography/errors.rb | 6 ++++-- spec/integration/rest_batch_spec.rb | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index bdae7a4..ef207c4 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -45,7 +45,7 @@ def merge_options(options) query_path = configuration + path query_body = merge_options(options)[:body] log path, query_body do - evaluate_response(@client.send(action.to_sym, query_path, query_body, merge_options(options)[:headers])) + evaluate_response(@client.send(action.to_sym, query_path, query_body, merge_options(options)[:headers]), path, query_body) end end end @@ -112,7 +112,7 @@ def evaluate_chunk_response(response, result) return_result(code, result) end - def evaluate_response(response) + def evaluate_response(response, path, query_body) if response.http_header.request_uri.request_uri == "/db/data/batch" code, body, parsed = handle_batch(response) else @@ -120,7 +120,7 @@ def evaluate_response(response) body = response.body.force_encoding("UTF-8") parsed = false end - return_result(response, code, body, parsed) + return_result(response, code, body, parsed, path, query_body) end def handle_batch(response) @@ -135,7 +135,7 @@ def handle_batch(response) return code, body, true end - def return_result(response, code, body, parsed) + def return_result(response, code, body, parsed, path, query_body) case code when 200 @logger.debug "OK, created #{body}" if @log_enabled @@ -149,20 +149,23 @@ def return_result(response, code, body, parsed) @logger.debug "OK, no content returned" if @log_enabled nil when 400..500 - handle_4xx_500_response(response, code, body) + handle_4xx_500_response(response, code, body, path, query_body) nil end end - def handle_4xx_500_response(response, code, body) + def handle_4xx_500_response(response, code, body, path, query_body) + index = 0 + request = {:path => path, :body => query_body} if body.nil? or body == "" parsed_body = {"message" => "No error message returned from server.", "stacktrace" => "No stacktrace returned from server." } elsif body.is_a? Hash parsed_body = body elsif body.is_a? Array - body.each do |result| + body.each_with_index do |result, idx| if result["status"] >= 400 + index = idx parsed_body = result["body"] || result break end @@ -175,10 +178,10 @@ def handle_4xx_500_response(response, code, body) stacktrace = parsed_body["stacktrace"] @logger.error "#{response.dump} error: #{body}" if @log_enabled - raise_errors(code, parsed_body["exception"], message, stacktrace) + raise_errors(code, parsed_body["exception"], message, stacktrace, request, index) end - def raise_errors(code, exception, message, stacktrace) + def raise_errors(code, exception, message, stacktrace, request, index) error = nil case code when 401 @@ -201,7 +204,7 @@ def raise_errors(code, exception, message, stacktrace) NeographyError end - raise error.new(message, code, stacktrace) + raise error.new(message, code, stacktrace, request, index) end def parse_string_options(options) diff --git a/lib/neography/errors.rb b/lib/neography/errors.rb index d03a785..8b9ac49 100644 --- a/lib/neography/errors.rb +++ b/lib/neography/errors.rb @@ -1,12 +1,14 @@ module Neography class NeographyError < StandardError - attr_reader :message, :code, :stacktrace + attr_reader :message, :code, :stacktrace, :request, :index - def initialize(message = nil, code = nil, stacktrace = nil) + def initialize(message = nil, code = nil, stacktrace = nil, request = nil, index = 0) @message = message @code = code @stacktrace = stacktrace + @request = request + @index = index end end diff --git a/spec/integration/rest_batch_spec.rb b/spec/integration/rest_batch_spec.rb index 5ff0d3c..c9fdeae 100644 --- a/spec/integration/rest_batch_spec.rb +++ b/spec/integration/rest_batch_spec.rb @@ -482,6 +482,20 @@ [:create_node, {:street1=>"94437 Kemmer Crossing", :street2=>"Apt. 333", :city=>"Abshireton", :state=>"AA", :zip=>"65820", :_type=>"Address", :created_at=>1335269478}], [:create_relationship, "has", "{0}", "{2}", {}] }.to raise_error(Neography::NeographyError) + + begin + batch_result = @neo.batch [:create_unique_node, "person", "ssn", "000-00-0001", {:first_name=>"Jane", :last_name=>"Doe", :ssn=>"000-00-0001", :_type=>"Person", :created_at=>1335269478}], + [:add_node_to_index, "person_ssn", "ssn", "000-00-0001", "{0}"], + [:create_node, {:street1=>"94437 Kemmer Crossing", :street2=>"Apt. 333", :city=>"Abshireton", :state=>"AA", :zip=>"65820", :_type=>"Address", :created_at=>1335269478}], + [:create_relationship, "has", "{0}", "{2}", {}] + rescue Neography::NeographyError => e + e.message.should == "Not Found" + e.code.should == 404 + e.stacktrace.should be_nil + e.request[:path].should == "/batch" + e.request[:body].should_not be_nil + e.index.should == 3 + end end end