Skip to content

Commit

Permalink
Raise an error when REST API returns an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Sep 28, 2012
1 parent e59c8e9 commit d3d7752
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 118 deletions.
37 changes: 30 additions & 7 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,38 @@ def evaluate_response(response)
when 204
@logger.debug "OK, no content returned" if @log_enabled
nil
when 400
@logger.error "Invalid data sent #{body}" if @log_enabled
nil
when 404
@logger.error "Not Found #{body}" if @log_enabled
when 400...500
handle_4xx_response(code, body)
nil
end
end

def handle_4xx_response(code, body)
parsed_body = JSON.parse(body)
message = parsed_body["message"]
stacktrace = parsed_body["stacktrace"]

@logger.error "#{code} error: #{body}" if @log_enabled

case code
when 400, 404
case parsed_body["exception"]
when "SyntaxException" ; raise SyntaxException.new(message, code, stacktrace)
when "PropertyValueException" ; raise PropertyValueException.new(message, code, stacktrace)
when "BadInputException" ; raise BadInputException.new(message, code, stacktrace)
when "NodeNotFoundException" ; raise NodeNotFoundException.new(message, code, stacktrace)
when "NoSuchPropertyException" ; raise NoSuchPropertyException.new(message, code, stacktrace)
when "RelationshipNotFoundException" ; raise RelationshipNotFoundException.new(message, code, stacktrace)
when "NotFoundException" ; raise NotFoundException.new(message, code, stacktrace)
else
raise NeographyError.new(message, code, stacktrace)
end
when 401
raise UnauthorizedError.new(message, code, stacktrace)
when 409
@logger.error "Node could not be deleted (still has relationships?)" if @log_enabled
nil
raise OperationFailureException.new(message, code, stacktrace)
else
raise NeographyError.new(message, code, stacktrace)
end
end

Expand Down
41 changes: 41 additions & 0 deletions lib/neography/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Neography

class NeographyError < StandardError
attr_reader :message, :stacktrace

def initialize(message = nil, code = nil, stacktrace = nil)
@message = message
@stacktrace = stacktrace
end
end

# HTTP Authentication error
class UnauthorizedError < NeographyError; end

# the Neo4j server Exceptions returned by the REST API:

# A node could not be found
class NodeNotFoundException < NeographyError; end

# A node cannot be deleted because it has relationships
class OperationFailureException < NeographyError; end

# Properties can not be null
class PropertyValueException < NeographyError; end

# Trying to a delete a property that does not exist
class NoSuchPropertyException < NeographyError; end

# A relationship could not be found
class RelationshipNotFoundException < NeographyError; end

# Error during valid Cypher query
class BadInputException < NeographyError; end

# Invalid Cypher query syntax
class SyntaxException < NeographyError; end

# A path could not be found by node traversal
class NotFoundException < NeographyError; end

end
9 changes: 7 additions & 2 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ def load(node, db = Neography::Rest.new)
end

def del
self.neo_server.delete_node!(self.neo_id)
neo_server.delete_node!(self.neo_id)
end

def exist?
!self.neo_server.get_node(self.neo_id).nil?
begin
neo_server.get_node(self.neo_id)
true
rescue NodeNotFoundException
false
end
end

end
Expand Down
15 changes: 10 additions & 5 deletions lib/neography/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ def initialize(hash=nil, server=nil)
@rel_type = hash["type"]
neo_server = server
end

def neo_server
@neo_server ||= self.start_node.neo_server
end

def neo_server=(server)
@neo_server = server
end

def del
self.start_node.neo_server.delete_relationship(self.neo_id)
start_node.neo_server.delete_relationship(neo_id)
end

def exist?
!self.start_node.neo_server.get_relationship(self.neo_id).nil?
begin
start_node.neo_server.get_relationship(neo_id)
true
rescue Neography::RelationshipNotFoundException
false
end
end

def attributes
attrs = self.methods - OpenStruct.instance_methods - Neography::Relationship.instance_methods
attrs.values_at(*attrs.each_index.select {|i| i.even?})
Expand Down
3 changes: 3 additions & 0 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
require 'neography/rest/gremlin'
require 'neography/rest/batch'
require 'neography/rest/clean'

require 'neography/errors'

require 'neography/connection'

module Neography
Expand Down
4 changes: 3 additions & 1 deletion spec/integration/node_relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ def create_nodes
p2 = Neography::Node.create
new_rel = Neography::Relationship.create(:family, p1, p2)
new_rel.del
Neography::Relationship.load(new_rel).should be_nil
expect {
Neography::Relationship.load(new_rel)
}.to raise_error Neography::RelationshipNotFoundException
end
end

Expand Down
14 changes: 8 additions & 6 deletions spec/integration/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
existing_node.neo_id.should == new_node.neo_id
end

it "returns nil if it tries to load a node that does not exist" do
it "raises an error if it tries to load a node that does not exist" do
new_node = Neography::Node.create
fake_node = new_node.neo_id.to_i + 1000
existing_node = Neography::Node.load(fake_node)
existing_node.should be_nil
expect {
existing_node = Neography::Node.load(fake_node)
}.to raise_error Neography::NodeNotFoundException
end

it "can load a node that exists not on the default rest server" do
Expand All @@ -74,15 +75,16 @@
new_node = Neography::Node.create
node_id = new_node.neo_id
new_node.del
deleted_node = Neography::Node.load(node_id)
deleted_node.should be_nil
expect {
Neography::Node.load(node_id)
}.to raise_error Neography::NodeNotFoundException
end
end

describe "exists?" do
it "can tell if it exists" do
new_node = Neography::Node.create
new_node.exist?.should be_true
new_node.exist?.should be_true
end

it "can tell if does not exists" do
Expand Down
49 changes: 25 additions & 24 deletions spec/integration/rest_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
batch_result.first.should have_key("from")
batch_result.first["body"]["self"].split('/').last.should == new_node[:id]
end

it "can get multiple nodes" do
node1 = @neo.create_node
node1[:id] = node1["self"].split('/').last
Expand Down Expand Up @@ -133,7 +133,7 @@
batch_result.first["body"]["end"].split('/').last.should == node2["self"].split('/').last
batch_result.first["body"]["self"].should == new_relationship["self"]
end

it "can create a single relationship" do
node1 = @neo.create_node
node2 = @neo.create_node
Expand Down Expand Up @@ -205,14 +205,13 @@
new_node = @neo.create_node
key = generate_text(6)
value = generate_text
new_index = @neo.get_node_index(index_name, key, value)
batch_result = @neo.batch [:add_node_to_index, index_name, key, value, new_node]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
existing_index = @neo.find_node_index(index_name, key, value)
existing_index = @neo.find_node_index(index_name, key, value)
existing_index.should_not be_nil
existing_index.first["self"].should == new_node["self"]
@neo.remove_node_from_index(index_name, key, value, new_node)
@neo.remove_node_from_index(index_name, key, value, new_node)
end

it "can get a node index" do
Expand Down Expand Up @@ -251,7 +250,7 @@
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
batch_result.first["body"]["self"].split('/').last.should == "0"
end
end

it "can batch gremlin with parameters" do
new_node = @neo.create_node
Expand All @@ -260,14 +259,14 @@
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
batch_result.first["body"]["self"].split('/').last.should == id
end
end

it "can batch cypher" do
batch_result = @neo.batch [:execute_query, "start n=node(0) return n"]
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
batch_result.first["body"]["data"][0][0]["self"].split('/').last.should == "0"
end
end

it "can batch cypher with parameters" do
new_node = @neo.create_node
Expand All @@ -276,35 +275,37 @@
batch_result.first.should have_key("id")
batch_result.first.should have_key("from")
batch_result.first["body"]["data"][0][0]["self"].split('/').last.should == id
end
end

it "can delete a node in batch" do

node1 = @neo.create_node
node2 = @neo.create_node
id1 = node1['self'].split('/').last
id2 = node2['self'].split('/').last
batch_result = @neo.batch [:delete_node, id1 ], [:delete_node, id2]
@neo.get_node(node1).should be_nil
@neo.get_node(node2).should be_nil

expect {
@neo.get_node(node1).should be_nil
}.to raise_error Neography::NodeNotFoundException
expect {
@neo.get_node(node2).should be_nil
}.to raise_error Neography::NodeNotFoundException
end

it "can remove a node from an index in batch " do
index = generate_text(6)
key = generate_text(6)
value1 = generate_text
value2 = generate_text
value3 = generate_text
value3 = generate_text

node1 = @neo.create_unique_node(index, key, value1, { "name" => "Max" })
node2 = @neo.create_unique_node(index, key, value2, { "name" => "Neo" })
node3 = @neo.create_unique_node(index, key, value3, { "name" => "Samir"})

batch_result = @neo.batch [:remove_node_from_index, index, key, value1, node1 ],
[:remove_node_from_index, index, key, node2 ],
[:remove_node_from_index, index, node3 ]

@neo.get_node_index(index, key, value1).should be_nil
@neo.get_node_index(index, key, value2).should be_nil
@neo.get_node_index(index, key, value3).should be_nil
Expand Down Expand Up @@ -398,12 +399,12 @@
key = generate_text(6)
value = generate_text

batch_result = @neo.batch [:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}],
batch_result = @neo.batch [:create_node, {"name" => "Max"}],
[:create_node, {"name" => "Marc"}],
[:add_node_to_index, "test_node_index", key, value, "{0}"]
[:add_node_to_index, "test_node_index", key, value, "{1}"]
[:create_relationship, "friends", "{0}", "{1}", {:since => "college"}]
[:add_relationship_to_index, "test_relationship_index", key, value, "{4}"]
[:create_relationship, "friends", "{0}", "{1}", {:since => "college"}]
[:add_relationship_to_index, "test_relationship_index", key, value, "{4}"]
batch_result.should_not be_nil
end

Expand All @@ -428,7 +429,7 @@
batch_result = @neo.batch [:create_node, {:street1=>"94437 Kemmer Crossing", :street2=>"Apt. 333", :city=>"Abshireton", :state=>"AA", :zip=>"65820", :_type=>"Address", :created_at=>1335269478}],
[:add_node_to_index, "person_ssn", "ssn", "000-00-0001", "{0}"],
[:create_unique_node, "person", "ssn", "000-00-0001", {:first_name=>"Jane", :last_name=>"Doe", :ssn=>"000-00-0001", :_type=>"Person", :created_at=>1335269478}],
[:create_relationship, "has", "{0}", "{2}", {}]
[:create_relationship, "has", "{0}", "{2}", {}]
batch_result.should_not be_nil


Expand All @@ -442,5 +443,5 @@
end

end

end

0 comments on commit d3d7752

Please sign in to comment.