Skip to content

Commit

Permalink
Merge pull request #16 from apolloio/shivam/passing-es-4xx-errors-to-…
Browse files Browse the repository at this point in the history
…response

[PLAT-695] Passing error down to Chewy Response to be accessible
  • Loading branch information
Shivam010 authored Nov 4, 2024
2 parents 754c612 + 65a00d1 commit f2dace4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/chewy/search/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Request

delegate :hits, :wrappers, :objects, :records, :documents,
:object_hash, :record_hash, :document_hash,
:total, :max_score, :took, :timed_out?, to: :response
:total, :max_score, :took, :timed_out?, :errors, to: :response
delegate :each, :size, :to_a, :[], to: :wrappers
alias_method :to_ary, :to_a
alias_method :total_count, :total
Expand Down Expand Up @@ -990,8 +990,9 @@ def perform(additional = {})
request_body.merge!({opaque_id: @x_opaque_id})
end
Chewy.client(_indices.first.hosts_name).search(request_body)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
{}
rescue Elasticsearch::Transport::Transport::Errors::NotFound => error
# passing error as a separate param down to the response, hence won't affect any other logic
{ "not_found_error" => error }
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/chewy/search/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def hits
@hits ||= hits_root['hits'] || []
end

# Raw response body obtained from ES.
# @return [Hash, nil]
def body
@body
end

# Response `total` field. Returns `0` if something went wrong.
#
# @return [Integer]
Expand All @@ -33,6 +39,21 @@ def max_score
@max_score ||= hits_root['max_score']
end

# Response `errors` field. Returns `nil` if there is no error.
# @return [Hash, nil]
def errors
return nil if @body.blank?

not_found_error = @body['not_found_error']
# there's another case when failures could be present in some shards, in that case es returns them
# in @body[_shards][failures] array
shard_failures = @body['_shards']['failures'] if @body['_shards']
{
not_found_error: not_found_error,
shard_failures: shard_failures
}.compact.presence
end

# Duration of the request handling in ms according to ES.
#
# @return [Integer]
Expand Down

0 comments on commit f2dace4

Please sign in to comment.