Skip to content

Commit

Permalink
adding request and index to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Jan 21, 2014
1 parent df1a113 commit d4aecae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
23 changes: 13 additions & 10 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,15 +112,15 @@ 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
code = response.code
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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/neography/errors.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 14 additions & 0 deletions spec/integration/rest_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4aecae

Please sign in to comment.