Skip to content

Commit

Permalink
[CLIENT] Fixed an error where exception was raised too late for error…
Browse files Browse the repository at this point in the history
… responses

Closes #40
  • Loading branch information
karmi committed Mar 4, 2014
1 parent 6274467 commit bff153a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,19 @@ def perform_request(method, path, params={}, body=nil, &block)
raise e
end

if response.status.to_i >= 300
__log_failed response if logger
__raise_transport_error response
end

json = serializer.load(response.body) if response.headers && response.headers["content-type"] =~ /json/
took = (json['took'] ? sprintf('%.3fs', json['took']/1000.0) : 'n/a') rescue 'n/a' if logger || tracer
duration = Time.now-start if logger || tracer

__log method, path, params, body, url, response, json, took, duration if logger
__trace method, path, params, body, url, response, json, took, duration if tracer

if response.status.to_i >= 300
__log_failed response if logger
__raise_transport_error response
else
Response.new response.status, json || response.body, response.headers
end
Response.new response.status, json || response.body, response.headers
ensure
@last_request_at = Time.now
end
Expand Down
9 changes: 9 additions & 0 deletions elasticsearch-transport/test/unit/transport_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ def initialize(*); end
end
end

should "raise an error for HTTP status 404 with application/json content type" do
@transport.expects(:get_connection).returns(stub_everything :failures => 1)
assert_raise Elasticsearch::Transport::Transport::Errors::NotFound do
@transport.perform_request 'GET', '/' do
Elasticsearch::Transport::Transport::Response.new 404, 'NOT FOUND', {"content-type" => 'application/json'}
end
end
end

should "raise an error on server failure" do
@transport.expects(:get_connection).returns(stub_everything :failures => 1)
assert_raise Elasticsearch::Transport::Transport::Errors::InternalServerError do
Expand Down

0 comments on commit bff153a

Please sign in to comment.