Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unifies NotFound error handling #126

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions elasticsearch-api/lib/elasticsearch/api/actions/exists.rb
Expand Up @@ -40,13 +40,9 @@ def exists(arguments={})
params = Utils.__validate_and_extract_params arguments, valid_params
body = nil

perform_request(method, path, params, body).status == 200 ? true : false
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
end
Utils.__rescue_from_not_found do
perform_request(method, path, params, body).status == 200 ? true : false
end
end

alias_method :exists?, :exists
Expand Down
Expand Up @@ -42,12 +42,8 @@ def exists(arguments={})
params = Utils.__validate_and_extract_params arguments, valid_params
body = nil

perform_request(method, path, params, body).status == 200 ? true : false
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
Utils.__rescue_from_not_found do
perform_request(method, path, params, body).status == 200 ? true : false
end
end

Expand Down
Expand Up @@ -40,12 +40,8 @@ def exists_alias(arguments={})
params = Utils.__validate_and_extract_params arguments, valid_params
body = nil

perform_request(method, path, params, body).status == 200 ? true : false
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
Utils.__rescue_from_not_found do
perform_request(method, path, params, body).status == 200 ? true : false
end
end

Expand Down
Expand Up @@ -21,12 +21,8 @@ def exists_template(arguments={})
params = Utils.__validate_and_extract_params arguments, valid_params
body = nil

perform_request(method, path, params, body).status == 200 ? true : false
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
Utils.__rescue_from_not_found do
perform_request(method, path, params, body).status == 200 ? true : false
end
end
end
Expand Down
Expand Up @@ -39,12 +39,8 @@ def exists_type(arguments={})
params = Utils.__validate_and_extract_params arguments, valid_params
body = nil

perform_request(method, path, params, body).status == 200 ? true : false
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
Utils.__rescue_from_not_found do
perform_request(method, path, params, body).status == 200 ? true : false
end
end

Expand Down
10 changes: 3 additions & 7 deletions elasticsearch-api/lib/elasticsearch/api/actions/ping.rb
Expand Up @@ -16,13 +16,9 @@ def ping(arguments={})
params = {}
body = nil

perform_request(method, path, params, body).status == 200 ? true : false
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
end
Utils.__rescue_from_not_found do
perform_request(method, path, params, body).status == 200 ? true : false
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions elasticsearch-api/lib/elasticsearch/api/utils.rb
Expand Up @@ -178,6 +178,23 @@ def __extract_parts(arguments, valid_parts=[])
return parts
end

# Calls given block, rescuing from any exceptions. Returns `false`
# if exception contains NotFound/404 in its class name or message, else re-raises exception.
#
# @yield [block] A block of code to be executed with exception handling.
#
# @api private
#
def __rescue_from_not_found(&block)
yield
rescue Exception => e
if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
false
else
raise e
end
end

extend self
end
end
Expand Down
22 changes: 22 additions & 0 deletions elasticsearch-api/test/unit/utils_test.rb
Expand Up @@ -200,6 +200,28 @@ class UtilsTest < ::Test::Unit::TestCase

end

context "__rescue_from_not_found" do

should "return false if exception class name contains 'NotFound'" do
assert_equal( false, __rescue_from_not_found { raise NotFound })
end

should "return false if exception message contains 'Not Found'" do
assert_equal( false, __rescue_from_not_found { raise Exception.new "Not Found" })
end

should "return false if exception message contains '404'" do
assert_equal( false, __rescue_from_not_found { raise Exception.new "404" })
end

should "raise exception if exception class name and message do not contain NotFound/404" do
assert_raise Exception do
__rescue_from_not_found { raise Exception.new "Any other exception" }
end
end

end

end
end
end
Expand Down