Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/orientdb4r.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Binary
autoload :RoundRobin, 'orientdb4r/load_balancing'
autoload :DocumentMetadata, 'orientdb4r/rest/model'

MUTEX_CLIENT = Mutex.new

class << self

Expand All @@ -42,7 +43,7 @@ def client options={}
return options.delete(:binary) ? Binary::BinClient.new(options) : RestClient.new(options)
end

Thread.exclusive {
MUTEX_CLIENT.synchronize {
client = options.delete(:binary) ? Binary::BinClient.new(options) : RestClient.new(options)
Thread.current[:orientdb_client] ||= client
#Thread.current[:orientdb_client] ||= BinClient.new options
Expand All @@ -62,10 +63,18 @@ class OrientdbError < StandardError
include ChainedError
end

###
# Error indicating that the request is malformed of invalid somehow.
class InvalidRequestError < OrientdbError; end

###
# Error indicating that access to the resource requires user authentication.
class UnauthorizedError < OrientdbError; end

###
# Error indicating a conflict between database state and the request
class StateConflictError < OrientdbError; end

###
# Error indicating that the server encountered an unexpected condition which prevented it from fulfilling the request.
class ServerError < OrientdbError; end
Expand Down
17 changes: 13 additions & 4 deletions lib/orientdb4r/rest/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,7 @@ def get_class(name) #:nodoc:
raise ArgumentError, "class name is blank" if blank?(name)

response = call_server(:method => :get, :uri => "class/#{@database}/#{name}")
rslt = process_response(response) do
raise NotFoundError, 'class not found' if response.body =~ /Invalid class/
end
rslt = process_response(response)

classes = [rslt]
decorate_classes_with_model(classes)
Expand All @@ -348,6 +346,8 @@ def get_class(name) #:nodoc:
end

clazz
rescue NotFoundError
raise NotFoundError, 'class not found'
end


Expand Down Expand Up @@ -423,8 +423,14 @@ def process_response(response)


# return code
if 401 == response.code
if 400 == response.code
raise InvalidRequestError, compose_error_message(response)
elsif 401 == response.code
raise UnauthorizedError, compose_error_message(response)
elsif 404 == response.code
raise NotFoundError, compose_error_message(response)
elsif 409 == response.code
raise StateConflictError, compose_error_message(response)
elsif 500 == response.code
raise ServerError, compose_error_message(response)
elsif 2 != (response.code / 100)
Expand Down Expand Up @@ -455,6 +461,9 @@ def process_response(response)
# correspond with expectation.
def compose_error_message(http_response, max_len=200)
msg = http_response.body.gsub("\n", ' ')
if (matcher = msg.match(/"content": "([^"]+)"/))
msg = matcher[1]
end
msg = "#{msg[0..max_len]} ..." if msg.size > max_len
msg
end
Expand Down
2 changes: 1 addition & 1 deletion lib/orientdb4r/rest/restclient_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def request(options) #:nodoc:

# store session ID if received to reuse in next request
sessid = response.cookies[SESSION_COOKIE_NAME]
if session_id != sessid and use_session
if sessid and session_id != sessid and use_session
@session_id = sessid
Orientdb4r::logger.debug "new session id: #{session_id}"
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_create_database
@client.get_database :database => 'UniT', :user => 'admin', :password => 'admin'
end
# creating an existing DB
assert_raise Orientdb4r::ServerError do
assert_raise Orientdb4r::StateConflictError do
@client.create_database :database => 'UniT', :user => 'root', :password => DB_ROOT_PASS
end
# insufficient rights
Expand Down
2 changes: 1 addition & 1 deletion test/test_ddo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_create_class_extends
assert_equal 'OUser', clazz.super_class

# bad super class
assert_raise Orientdb4r::ServerError do @client.create_class(CLASS, :extends => 'nonExistingSuperClass'); end
assert_raise Orientdb4r::InvalidRequestError do @client.create_class(CLASS, :extends => 'nonExistingSuperClass'); end
end


Expand Down
6 changes: 3 additions & 3 deletions test/test_dmo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_insert
assert_equal urids.size, @client.query("SELECT FROM #{CLASS} WHERE prop2 = 'linkset'")[0]['friends'].size

# table doesn't exist
assert_raise Orientdb4r::ServerError do
assert_raise Orientdb4r::InvalidRequestError do
@client.command "INSERT INTO #{CLASS}x (prop1, prop2, friends) VALUES (1, 'linkset', [#{urids.join(',')}])"
end
# bad syntax
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_query
assert_equal 1, gr.select { |e| e if e['@class'] == 'ORole' }.size

# table doesn't exist
assert_raise Orientdb4r::ServerError do
assert_raise Orientdb4r::InvalidRequestError do
@client.query 'SELECT FROM OUserX'
end
# bad syntax
Expand All @@ -105,7 +105,7 @@ def test_query
assert_instance_of Array, entries
assert entries.empty?
# try to find entry in a non-existing cluster
assert_raise Orientdb4r::ServerError do @client.query 'SELECT FROM #111:1111'; end
assert_raise Orientdb4r::NotFoundError do @client.query 'SELECT FROM #111:1111'; end
# used for INSERT
assert_raise Orientdb4r::ServerError do
@client.query "INSERT INTO #{CLASS} (prop1, prop2, friends) VALUES (0, 'string0', [])"
Expand Down
2 changes: 1 addition & 1 deletion test/test_document_crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_batch_bad_params
doc = @client.create_document( { '@class' => CLASS, 'prop1' => 1, 'prop2' => 'text' })
assert_raise Orientdb4r::ServerError do @client.batch(nil); end
assert_raise Orientdb4r::ServerError do @client.batch({:foo => :baz, :bar => :alfa}); end # bad structure
assert_raise Orientdb4r::ServerError do @client.batch({:operations => [{:type => :d, :record => {'@rid' => '#123:456'}}]}); end # bad cluster
assert_raise Orientdb4r::NotFoundError do @client.batch({:operations => [{:type => :d, :record => {'@rid' => '#123:456'}}]}); end # bad cluster
assert_nothing_thrown do @client.batch({:operations => [{:type => :d, :record => {'@rid' => "##{doc.doc_rid.cluster_id}:678"}}]}); end # bad RID is not problem
end

Expand Down