From ff613916a8064c4922172651942710dcb7879050 Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Fri, 1 Feb 2013 12:12:42 -0700 Subject: [PATCH 1/2] handle json parsing error for empty return body a 404 message can be returned from the server without having a message in it (when deleting from an index with key, value and id and an empty string is contained in the value) --- lib/neography/connection.rb | 12 +++++++++--- spec/integration/rest_index_spec.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index 3805451..710e5eb 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -105,9 +105,15 @@ def evaluate_response(response) end def handle_4xx_500_response(code, body) - parsed_body = JSON.parse(body) - message = parsed_body["message"] - stacktrace = parsed_body["stacktrace"] + if body + parsed_body = JSON.parse(body) + message = parsed_body["message"] + stacktrace = parsed_body["stacktrace"] + else + parsed_body = {} + message = "No error message returned from server." + stacktrace = "" + end @logger.error "#{code} error: #{body}" if @log_enabled diff --git a/spec/integration/rest_index_spec.rb b/spec/integration/rest_index_spec.rb index 2038b51..956b59a 100644 --- a/spec/integration/rest_index_spec.rb +++ b/spec/integration/rest_index_spec.rb @@ -167,6 +167,16 @@ new_index = @neo.get_relationship_index("test_relationship_index", key, value) new_index.should be_nil end + + it "throws an error when there is an empty string in the value when removing a node" do + new_node = @neo.create_node + key = generate_text(6) + value = generate_text + @neo.add_node_to_index("test_node_index", key, value, new_node) + new_index = @neo.get_node_index("test_node_index", key, value) + new_index.should_not be_nil + expect { @neo.remove_node_from_index("test_node_index", key, "", new_node) }.to raise_error + end end describe "get index" do From dc99e62065cb2cb9f29d85bb1e2c475a64bd64de Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Fri, 1 Feb 2013 12:26:46 -0700 Subject: [PATCH 2/2] Fixed error test Needed to specify which error in the spec. Fixed code so it works properly with the correct error now --- lib/neography/connection.rb | 10 +++++----- spec/integration/rest_index_spec.rb | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index 710e5eb..088ea94 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -105,14 +105,14 @@ def evaluate_response(response) end def handle_4xx_500_response(code, body) - if body - parsed_body = JSON.parse(body) - message = parsed_body["message"] - stacktrace = parsed_body["stacktrace"] - else + if body.nil? or body == "" parsed_body = {} message = "No error message returned from server." stacktrace = "" + else + parsed_body = JSON.parse(body) + message = parsed_body["message"] + stacktrace = parsed_body["stacktrace"] end @logger.error "#{code} error: #{body}" if @log_enabled diff --git a/spec/integration/rest_index_spec.rb b/spec/integration/rest_index_spec.rb index 956b59a..6e70797 100644 --- a/spec/integration/rest_index_spec.rb +++ b/spec/integration/rest_index_spec.rb @@ -175,7 +175,7 @@ @neo.add_node_to_index("test_node_index", key, value, new_node) new_index = @neo.get_node_index("test_node_index", key, value) new_index.should_not be_nil - expect { @neo.remove_node_from_index("test_node_index", key, "", new_node) }.to raise_error + expect { @neo.remove_node_from_index("test_node_index", key, "", new_node) }.to raise_error Neography::NeographyError end end